home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / ange-ftp.el < prev    next >
Text File  |  1996-08-04  |  215KB  |  5,474 lines

  1. ;;; ange-ftp.el --- transparent FTP support for GNU Emacs
  2.  
  3. ;;; Modified for emx by Eberhard Mattes, Aug 1996
  4.  
  5. ;; Copyright (C) 1989,90,91,92,93,94,95,96  Free Software Foundation, Inc.
  6.  
  7. ;; Author: Andy Norman (ange@hplb.hpl.hp.com)
  8. ;; Maintainer: FSF
  9. ;; Keywords: comm
  10.  
  11. ;; This file is part of GNU Emacs.
  12.  
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  25. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;; Boston, MA 02111-1307, USA.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; This package attempts to make accessing files and directories using FTP
  31. ;; from within GNU Emacs as simple and transparent as possible.  A subset of
  32. ;; the common file-handling routines are extended to interact with FTP.
  33.  
  34. ;; Usage:
  35. ;;
  36. ;; Some of the common GNU Emacs file-handling operations have been made
  37. ;; FTP-smart.  If one of these routines is given a filename that matches
  38. ;; '/user@host:name' then it will spawn an FTP process connecting to machine
  39. ;; 'host' as account 'user' and perform its operation on the file 'name'.
  40. ;;
  41. ;; For example: if find-file is given a filename of:
  42. ;;
  43. ;;   /ange@anorman:/tmp/notes
  44. ;;
  45. ;; then ange-ftp spawns an FTP process, connect to the host 'anorman' as
  46. ;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the
  47. ;; contents of that file as if it were on the local filesystem.  If ange-ftp
  48. ;; needs a password to connect then it reads one in the echo area.
  49.  
  50. ;; Extended filename syntax:
  51. ;;
  52. ;; The default extended filename syntax is '/user@host:name', where the
  53. ;; 'user@' part may be omitted.  This syntax can be customised to a certain
  54. ;; extent by changing ange-ftp-name-format.  There are limitations.
  55. ;;
  56. ;; If the user part is omitted then ange-ftp generates a default user
  57. ;; instead whose value depends on the variable ange-ftp-default-user.
  58.  
  59. ;; Passwords:
  60. ;;
  61. ;; A password is required for each host/user pair.  Ange-ftp reads passwords
  62. ;; as needed.  You can also specify a password with ange-ftp-set-passwd, or
  63. ;; in a *valid* ~/.netrc file.
  64.  
  65. ;; Passwords for user "anonymous":
  66. ;;
  67. ;; Passwords for the user "anonymous" (or "ftp") are handled
  68. ;; specially.  The variable `ange-ftp-generate-anonymous-password'
  69. ;; controls what happens: if the value of this variable is a string,
  70. ;; then this is used as the password; if non-nil (the default), then
  71. ;; the value of `user-mail-address' is used; if nil then the user
  72. ;; is prompted for a password as normal.
  73.  
  74. ;; "Dumb" UNIX hosts:
  75. ;;
  76. ;; The FTP servers on some UNIX machines have problems if the 'ls' command is
  77. ;; used.
  78. ;;
  79. ;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to
  80. ;; limit itself to the DIR command and not 'ls' for a given UNIX host.  Note
  81. ;; that this change will take effect for the current GNU Emacs session only.
  82. ;; See below for a discussion of non-UNIX hosts.  If a large number of
  83. ;; machines with similar hostnames have this problem then it is easier to set
  84. ;; the value of ange-ftp-dumb-unix-host-regexp in your .emacs file. ange-ftp
  85. ;; is unable to automatically recognize dumb unix hosts.
  86.  
  87. ;; File name completion:
  88. ;;
  89. ;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts.
  90. ;; To do filename completion, ange-ftp needs a listing from the remote host.
  91. ;; Therefore, for very slow connections, it might not save any time.
  92.  
  93. ;; FTP processes:
  94. ;;
  95. ;; When ange-ftp starts up an FTP process, it leaves it running for speed
  96. ;; purposes.  Some FTP servers will close the connection after a period of
  97. ;; time, but ange-ftp should be able to quietly reconnect the next time that
  98. ;; the process is needed.
  99. ;;
  100. ;; Killing the "*ftp user@host*" buffer also kills the ftp process.
  101. ;; This should not cause ange-ftp any grief.
  102.  
  103. ;; Binary file transfers:
  104. ;;
  105. ;; By default ange-ftp transfers files in ASCII mode.  If a file being
  106. ;; transferred matches the value of ange-ftp-binary-file-name-regexp then
  107. ;; binary mode is used for that transfer.
  108.  
  109. ;; Account passwords:
  110. ;;
  111. ;; Some FTP servers require an additional password which is sent by the
  112. ;; ACCOUNT command.  ange-ftp partially supports this by allowing the user to
  113. ;; specify an account password by either calling ange-ftp-set-account, or by
  114. ;; specifying an account token in the .netrc file.  If the account password
  115. ;; is set by either of these methods then ange-ftp will issue an ACCOUNT
  116. ;; command upon starting the FTP process.
  117.  
  118. ;; Preloading:
  119. ;;
  120. ;; ange-ftp can be preloaded, but must be put in the site-init.el file and
  121. ;; not the site-load.el file in order for the documentation strings for the
  122. ;; functions being overloaded to be available.
  123.  
  124. ;; Status reports:
  125. ;;
  126. ;; Most ange-ftp commands that talk to the FTP process output a status
  127. ;; message on what they are doing.  In addition, ange-ftp can take advantage
  128. ;; of the FTP client's HASH command to display the status of transferring
  129. ;; files and listing directories.  See the documentation for the variables
  130. ;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and
  131. ;; ange-ftp-process-verbose for more details.
  132.  
  133. ;; Gateways:
  134. ;;
  135. ;; Sometimes it is necessary for the FTP process to be run on a different
  136. ;; machine than the machine running GNU Emacs.  This can happen when the
  137. ;; local machine has restrictions on what hosts it can access.
  138. ;;
  139. ;; ange-ftp has support for running the ftp process on a different (gateway)
  140. ;; machine.  The way it works is as follows:
  141. ;;
  142. ;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine
  143. ;;    that doesn't have the access restrictions.
  144. ;;
  145. ;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression
  146. ;;    that matches hosts that can be contacted from running a local ftp
  147. ;;    process, but fails to match hosts that can't be accessed locally.  For
  148. ;;    example:
  149. ;;
  150. ;;    "\\.hp\\.com$\\|^[^.]*$"
  151. ;;
  152. ;;    will match all hosts that are in the .hp.com domain, or don't have an
  153. ;;    explicit domain in their name, but will fail to match hosts with
  154. ;;    explicit domains or that are specified by their ip address.
  155. ;;
  156. ;; 3) Using NFS and symlinks, make sure that there is a shared directory with
  157. ;;    the *same* name between the local machine and the gateway machine.
  158. ;;    This directory is necessary for temporary files created by ange-ftp.
  159. ;;
  160. ;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of
  161. ;;    this directory plus an identifying filename prefix.  For example:
  162. ;;
  163. ;;    "/nfs/hplose/ange/ange-ftp"
  164. ;;
  165. ;;    where /nfs/hplose/ange is a directory that is shared between the
  166. ;;    gateway machine and the local machine.
  167. ;;
  168. ;; The simplest way of getting a ftp process running on the gateway machine
  169. ;; is if you can spawn a remote shell using either 'rsh' or 'remsh'.  If you
  170. ;; can't do this for some reason such as security then points 7 onwards will
  171. ;; discuss an alternative approach.
  172. ;;
  173. ;; 5) Set the variable ange-ftp-gateway-program to the name of the remote
  174. ;;    shell process such as 'remsh' or 'rsh' if the default isn't correct.
  175. ;;
  176. ;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it
  177. ;;    isn't already.  This tells ange-ftp that you are using a remote shell
  178. ;;    rather than logging in using telnet or rlogin.
  179. ;;
  180. ;; That should be all you need to allow ange-ftp to spawn a ftp process on
  181. ;; the gateway machine.  If you have to use telnet or rlogin to get to the
  182. ;; gateway machine then follow the instructions below.
  183. ;;
  184. ;; 7) Set the variable ange-ftp-gateway-program to the name of the program
  185. ;;    that lets you log onto the gateway machine.  This may be something like
  186. ;;    telnet or rlogin.
  187. ;;
  188. ;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular
  189. ;;    expression that matches the prompt you get when you login to the
  190. ;;    gateway machine.  Be very specific here; this regexp must not match
  191. ;;    *anything* in your login banner except this prompt.
  192. ;;    shell-prompt-pattern is far too general as it appears to match some
  193. ;;    login banners from Sun machines.  For example:
  194. ;;
  195. ;;    "^$*$ *"
  196. ;;
  197. ;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let
  198. ;;    ange-ftp know that it has to "hand-hold" the login to the gateway
  199. ;;    machine.
  200. ;;
  201. ;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command
  202. ;;     that will put the pty connected to the gateway machine into a
  203. ;;     no-echoing mode, and will strip off carriage-returns from output from
  204. ;;     the gateway machine.  For example:
  205. ;;
  206. ;;     "stty -onlcr -echo"
  207. ;;
  208. ;;     will work on HP-UX machines, whereas:
  209. ;;
  210. ;;     "stty -echo nl"
  211. ;;
  212. ;;     appears to work for some Sun machines.
  213. ;;
  214. ;; That's all there is to it.
  215.  
  216. ;; Smart gateways:
  217. ;;
  218. ;; If you have a "smart" ftp program that allows you to issue commands like
  219. ;; "USER foo@bar" which do nice proxy things, then look at the variables
  220. ;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port.
  221. ;;
  222. ;; Otherwise, if there is an alternate ftp program that implements proxy in
  223. ;; a transparent way (i.e. w/o specifying the proxy host), that will
  224. ;; connect you directly to the desired destination host:
  225. ;; Set ange-ftp-gateway-ftp-program-name to that program's name.
  226. ;; Set ange-ftp-local-host-regexp to a value as stated earlier on.
  227. ;; Leave ange-ftp-gateway-host set to nil.
  228. ;; Set ange-ftp-smart-gateway to t.
  229.  
  230. ;; Tips for using ange-ftp:
  231. ;;
  232. ;; 1. For dired to work on a host which marks symlinks with a trailing @ in
  233. ;;    an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t).
  234. ;;    Most UNIX systems do not do this, but ULTRIX does. If you think that
  235. ;;    there is a chance you might connect to an ULTRIX machine (such as
  236. ;;    prep.ai.mit.edu), then set this variable accordingly.  This will have
  237. ;;    the side effect that dired will have problems with symlinks whose names
  238. ;;    end in an @.  If you get yourself into this situation then editing
  239. ;;    dired's ls-switches to remove "F", will temporarily fix things.
  240. ;;
  241. ;; 2. If you know that you are connecting to a certain non-UNIX machine
  242. ;;    frequently, and ange-ftp seems to be unable to guess its host-type,
  243. ;;    then setting the appropriate host-type regexp
  244. ;;    (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or
  245. ;;    ange-ftp-cms-host-regexp) accordingly should help. Also, please report
  246. ;;    ange-ftp's inability to recognize the host-type as a bug.
  247. ;;
  248. ;; 3. For slow connections, you might get "listing unreadable" error
  249. ;;    messages, or get an empty buffer for a file that you know has something
  250. ;;    in it. The solution is to increase the value of ange-ftp-retry-time.
  251. ;;    Its default value is 5 which is plenty for reasonable connections.
  252. ;;    However, for some transatlantic connections I set this to 20.
  253. ;;
  254. ;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by
  255. ;;    copying the file to the local machine, compressing it there, and then
  256. ;;    sending it back. Binary file transfers between machines of different
  257. ;;    architectures can be a risky business. Test things out first on some
  258. ;;    test files. See "Bugs" below. Also, note that ange-ftp copies files by
  259. ;;    moving them through the local machine. Again, be careful when doing
  260. ;;    this with binary files on non-Unix machines.
  261. ;;
  262. ;; 5. Beware that dired over ftp will use your setting of dired-no-confirm
  263. ;;    (list of dired commands for which confirmation is not asked).  You
  264. ;;    might want to reconsider your setting of this variable, because you
  265. ;;    might want confirmation for more commands on remote direds than on
  266. ;;    local direds. For example, I strongly recommend that you not include
  267. ;;    compress and uncompress in this list. If there is enough demand it
  268. ;;    might be a good idea to have an alist ange-ftp-dired-no-confirm of
  269. ;;    pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST
  270. ;;    is a list of commands for which confirmation would be suppressed.  Then
  271. ;;    remote dired listings would take their (buffer-local) value of
  272. ;;    dired-no-confirm from this alist. Who votes for this?
  273.  
  274. ;; ---------------------------------------------------------------------
  275. ;; Non-UNIX support:
  276. ;; ---------------------------------------------------------------------
  277.  
  278. ;; VMS support:
  279. ;;
  280. ;; Ange-ftp has full support for VMS hosts.  It
  281. ;; should be able to automatically recognize any VMS machine. However, if it
  282. ;; fails to do this, you can use the command ange-ftp-add-vms-host.  As well,
  283. ;; you can set the variable ange-ftp-vms-host-regexp in your .emacs file. We
  284. ;; would be grateful if you would report any failures to automatically
  285. ;; recognize a VMS host as a bug.
  286. ;;
  287. ;; Filename Syntax:
  288. ;;
  289. ;; For ease of *implementation*, the user enters the VMS filename syntax in a
  290. ;; UNIX-y way.  For example:
  291. ;;  PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1
  292. ;; would be entered as:
  293. ;;  /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1
  294. ;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file:
  295. ;;  [.CSV.POLICY]RULES.MEM
  296. ;; you would type:
  297. ;;  C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM
  298. ;;
  299. ;; A legal VMS filename is of the form: FILE.TYPE;##
  300. ;; where FILE can be up to 39 characters
  301. ;;       TYPE can be up to 39 characters
  302. ;;       ## is a version number (an integer between 1 and 32,767)
  303. ;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $
  304. ;; $ cannot begin a filename, and - cannot be used as the first or last
  305. ;; character.
  306. ;;
  307. ;; Tips:
  308. ;; 1. Although VMS is not case sensitive, EMACS running under UNIX is.
  309. ;;    Therefore, to access a VMS file, you must enter the filename with upper
  310. ;;    case letters.
  311. ;; 2. To access the latest version of file under VMS, you use the filename
  312. ;;    without the ";" and version number. You should always edit the latest
  313. ;;    version of a file. If you want to edit an earlier version, copy it to a
  314. ;;    new file first. This has nothing to do with ange-ftp, but is simply
  315. ;;    good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is
  316. ;;    latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you
  317. ;;    inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find
  318. ;;    that VMS will not allow you to save the file because it will refuse to
  319. ;;    overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and
  320. ;;    attach the buffer to this file. To get out of this situation, M-x
  321. ;;    write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to
  322. ;;    latest version of the file. For this reason, in dired "f"
  323. ;;    (dired-find-file), always loads the file sans version, whereas "v",
  324. ;;    (dired-view-file), always loads the explicit version number. The
  325. ;;    reasoning being that it reasonable to view old versions of a file, but
  326. ;;    not to edit them.
  327. ;; 3. EMACS has a feature in which it does environment variable substitution
  328. ;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
  329. ;;    by typing $$.
  330.  
  331. ;; MTS support:
  332. ;;
  333. ;; Ange-ftp has full support for hosts running
  334. ;; the Michigan terminal system.  It should be able to automatically
  335. ;; recognize any MTS machine. However, if it fails to do this, you can use
  336. ;; the command ange-ftp-add-mts-host.  As well, you can set the variable
  337. ;; ange-ftp-mts-host-regexp in your .emacs file. We would be grateful if you
  338. ;; would report any failures to automatically recognize a MTS host as a bug.
  339. ;;
  340. ;; Filename syntax:
  341. ;; 
  342. ;; MTS filenames are entered in a UNIX-y way. For example, if your account
  343. ;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be
  344. ;; entered as
  345. ;;   /YYYY@mtsg.ubc.ca:/XXXX:/FILE
  346. ;; In other words, MTS accounts are treated as UNIX directories. Of course,
  347. ;; to access a file in another account, you must have access permission for
  348. ;; it.  If FILE were in your own account, then you could enter it in a
  349. ;; relative name fashion as
  350. ;;   /YYYY@mtsg.ubc.ca:FILE
  351. ;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the
  352. ;; filename does not contain a TYPE (i.e. it can have as many "."'s as you
  353. ;; like.) MTS filenames are always in upper case, and hence be sure to enter
  354. ;; them as such! MTS is not case sensitive, but an EMACS running under UNIX
  355. ;; is.
  356.  
  357. ;; CMS support:
  358. ;; 
  359. ;; Ange-ftp has full support for hosts running
  360. ;; CMS.  It should be able to automatically recognize any CMS machine.
  361. ;; However, if it fails to do this, you can use the command
  362. ;; ange-ftp-add-cms-host.  As well, you can set the variable
  363. ;; ange-ftp-cms-host-regexp in your .emacs file. We would be grateful if you
  364. ;; would report any failures to automatically recognize a CMS host as a bug.
  365. ;; 
  366. ;; Filename syntax:
  367. ;;
  368. ;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are
  369. ;; treated as UNIX directories. For example to access the file READ.ME in
  370. ;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter
  371. ;;   /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME
  372. ;; If *.301 is the default minidisk for this account, you could access
  373. ;; FOO.BAR on this minidisk as
  374. ;;   /anonymous@cuvmb.cc.columbia.edu:FOO.BAR
  375. ;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be
  376. ;; up to 8 characters. Again, beware that CMS filenames are always upper
  377. ;; case, and hence must be entered as such.
  378. ;;
  379. ;; Tips:
  380. ;; 1. CMS machines, with the exception of anonymous accounts, nearly always
  381. ;;    need an account password. To have ange-ftp send an account password,
  382. ;;    you can either include it in your .netrc file, or use
  383. ;;    ange-ftp-set-account.
  384. ;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we
  385. ;;    can fix this.
  386. ;;
  387. ;; ------------------------------------------------------------------
  388. ;; Bugs:
  389. ;; ------------------------------------------------------------------
  390. ;; 
  391. ;; 1. Umask problems:
  392. ;;    Be warned that files created by using ange-ftp will take account of the
  393. ;;    umask of the ftp daemon process rather than the umask of the creating
  394. ;;    user.  This is particularly important when logging in as the root user.
  395. ;;    The way that I tighten up the ftp daemon's umask under HP-UX is to make
  396. ;;    sure that the umask is changed to 027 before I spawn /etc/inetd.  I
  397. ;;    suspect that there is something similar on other systems.
  398. ;;
  399. ;; 2. Some combinations of FTP clients and servers break and get out of sync
  400. ;;    when asked to list a non-existent directory.  Some of the ai.mit.edu
  401. ;;    machines cause this problem for some FTP clients. Using
  402. ;;    ange-ftp-kill-ftp-process can restart the ftp process, which
  403. ;;    should get things back in sync.
  404. ;;
  405. ;; 3. Ange-ftp does not check to make sure that when creating a new file,
  406. ;;    you provide a valid filename for the remote operating system.
  407. ;;    If you do not, then the remote FTP server will most likely
  408. ;;    translate your filename in some way. This may cause ange-ftp to
  409. ;;    get confused about what exactly is the name of the file. The
  410. ;;    most common causes of this are using lower case filenames on systems
  411. ;;    which support only upper case, and using filenames which are too
  412. ;;    long.
  413. ;;
  414. ;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons.
  415. ;;
  416. ;; 5. Ange-ftp likes to use pty's to talk to its FTP processes.  If GNU Emacs
  417. ;;    for some reason creates a FTP process that only talks via pipes then
  418. ;;    ange-ftp won't be getting the information it requires at the time that
  419. ;;    it wants it since pipes flush at different times to pty's.  One
  420. ;;    disgusting way around this problem is to talk to the FTP process via
  421. ;;    rlogin which does the 'right' things with pty's.
  422. ;;
  423. ;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't
  424. ;;    worried about this too much. Eventually, we should have some caching
  425. ;;    of the current minidisk.
  426. ;;    
  427. ;; 7. Some CMS machines do not assign a default minidisk when you ftp them as
  428. ;;    anonymous. It is then necessary to guess a valid minidisk name, and cd
  429. ;;    to it. This is (understandably) beyond ange-ftp.
  430. ;;
  431. ;; 8. Remote to remote copying of files on non-Unix machines can be risky.
  432. ;;    Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp
  433. ;;    will use binary mode for the copy. Between systems of different
  434. ;;    architecture, this still may not be enough to guarantee the integrity
  435. ;;    of binary files. Binary file transfers from VMS machines are
  436. ;;    particularly problematical. Should ange-ftp-binary-file-name-regexp be
  437. ;;    an alist of OS type, regexp pairs?
  438. ;;
  439. ;; 9. The code to do compression of files over ftp is not as careful as it
  440. ;;    should be. It deletes the old remote version of the file, before
  441. ;;    actually checking if the local to remote transfer of the compressed
  442. ;;    file succeeds. Of course to delete the original version of the file
  443. ;;    after transferring the compressed version back is also dangerous,
  444. ;;    because some OS's have severe restrictions on the length of filenames,
  445. ;;    and when the compressed version is copied back the "-Z" or ".Z" may be
  446. ;;    truncated. Then, ange-ftp would delete the only remaining version of
  447. ;;    the file.  Maybe ange-ftp should make backups when it compresses files
  448. ;;    (of course, the backup "~" could also be truncated off, sigh...).
  449. ;;    Suggestions?
  450. ;;
  451. ;; 10. If a dir listing is attempted for an empty directory on (at least
  452. ;;     some) VMS hosts, an ftp error is given. This is really an ftp bug, and
  453. ;;     I don't know how to get ange-ftp work to around it.
  454. ;;
  455. ;; 11. Bombs on filenames that start with a space. Deals well with filenames
  456. ;;     containing spaces, but beware that the remote ftpd may not like them
  457. ;;     much.
  458. ;;
  459. ;; 12. The dired support for non-Unix-like systems does not currently work.
  460. ;;     It needs to be reimplemented by modifying the parse-...-listing
  461. ;;    functions to convert the directory listing to ls -l format.
  462. ;; 
  463. ;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks
  464. ;;     with a trailing @ in a ls -alF listing. In order to account for this
  465. ;;     ange-ftp looks to chop trailing @'s off of symlink names when it is
  466. ;;     parsing a listing with the F switch. This will cause ange-ftp to
  467. ;;     incorrectly get the name of a symlink on a non-ULTRIX host if its name
  468. ;;     ends in an @. ange-ftp will correct itself if you take F out of the
  469. ;;     dired ls switches (C-u s will allow you to edit the switches). The
  470. ;;     dired buffer will be automatically reverted, which will allow ange-ftp
  471. ;;     to fix its files hashtable.  A cookie to anyone who can think of a
  472. ;;     fast, sure-fire way to recognize ULTRIX over ftp.
  473.  
  474. ;; If you find any bugs or problems with this package, PLEASE either e-mail
  475. ;; the above author, or send a message to the ange-ftp-lovers mailing list
  476. ;; below.  Ideas and constructive comments are especially welcome.
  477.  
  478. ;; ange-ftp-lovers:
  479. ;;
  480. ;; ange-ftp has its own mailing list modestly called ange-ftp-lovers.  All
  481. ;; users of ange-ftp are welcome to subscribe (see below) and to discuss
  482. ;; aspects of ange-ftp.  New versions of ange-ftp are posted periodically to
  483. ;; the mailing list.
  484. ;;
  485. ;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the
  486. ;; list, please mail one of the following addresses:
  487. ;;
  488. ;;     ange-ftp-lovers-request@anorman.hpl.hp.com
  489. ;; or
  490. ;;     ange-ftp-lovers-request%anorman.hpl.hp.com@hplb.hpl.hp.com
  491. ;;
  492. ;; Please don't forget the -request part.
  493. ;;
  494. ;; For mail to be posted directly to ange-ftp-lovers, send to one of the
  495. ;; following addresses:
  496. ;; 
  497. ;;     ange-ftp-lovers@anorman.hpl.hp.com
  498. ;; or
  499. ;;     ange-ftp-lovers%anorman.hpl.hp.com@hplb.hpl.hp.com
  500. ;;
  501. ;; Alternatively, there is a mailing list that only gets announcements of new
  502. ;; ange-ftp releases.  This is called ange-ftp-lovers-announce, and can be
  503. ;; subscribed to by e-mailing to the -request address as above.  Please make
  504. ;; it clear in the request which mailing list you wish to join.
  505.  
  506. ;; The latest version of ange-ftp can usually be obtained via anonymous ftp
  507. ;; from:
  508. ;;     alpha.gnu.ai.mit.edu:ange-ftp/ange-ftp.tar.Z
  509. ;; or:
  510. ;;     ugle.unit.no:/pub/gnu/emacs-lisp/ange-ftp.tar.Z
  511. ;; or:
  512. ;;   archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/packages/ange-ftp.tar.Z
  513.  
  514. ;; The archives for ange-ftp-lovers can be found via anonymous ftp under:
  515. ;;
  516. ;;     ftp.reed.edu:pub/mailing-lists/ange-ftp/
  517.  
  518. ;; -----------------------------------------------------------
  519. ;; Technical information on this package:
  520. ;; -----------------------------------------------------------
  521.  
  522. ;; ange-ftp works by putting a handler on file-name-handler-alist
  523. ;; which is called by many primitives, and a few non-primitives,
  524. ;; whenever they see a file name of the appropriate sort.
  525.  
  526. ;; Checklist for adding non-UNIX support for TYPE
  527. ;; 
  528. ;; The following functions may need TYPE versions:
  529. ;; (not all functions will be needed for every OS)
  530. ;;
  531. ;; ange-ftp-fix-name-for-TYPE
  532. ;; ange-ftp-fix-dir-name-for-TYPE
  533. ;; ange-ftp-TYPE-host
  534. ;; ange-ftp-TYPE-add-host
  535. ;; ange-ftp-parse-TYPE-listing
  536. ;; ange-ftp-TYPE-delete-file-entry
  537. ;; ange-ftp-TYPE-add-file-entry
  538. ;; ange-ftp-TYPE-file-name-as-directory
  539. ;; ange-ftp-TYPE-make-compressed-filename
  540. ;; ange-ftp-TYPE-file-name-sans-versions
  541. ;;
  542. ;; Variables:
  543. ;;
  544. ;; ange-ftp-TYPE-host-regexp
  545. ;; May need to add TYPE to ange-ftp-dumb-host-types
  546. ;;
  547. ;; Check the following functions for OS dependent coding:
  548. ;;
  549. ;; ange-ftp-host-type
  550. ;; ange-ftp-guess-host-type
  551. ;; ange-ftp-allow-child-lookup
  552.  
  553. ;; Host type conventions:
  554. ;;
  555. ;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type
  556. ;; (mostly) follow the following conventions for remote host types.  At
  557. ;; least, I think that future code should try to follow these conventions,
  558. ;; and the current code should eventually be made compliant.
  559. ;;
  560. ;; nil = local host type, whatever that is (probably unix).
  561. ;;       Think nil as in "not a remote host". This value is used by
  562. ;;       ange-ftp-dired-host-type for local buffers.
  563. ;;
  564. ;; t = a remote host of unknown type. Think t is in true, it's remote.
  565. ;;     Currently, 'unix is used as the default remote host type.
  566. ;;     Maybe we should use t.
  567. ;;
  568. ;; 'type = a remote host of TYPE type.
  569. ;;
  570. ;; 'type:list = a remote host of TYPE type, using a specialized ftp listing
  571. ;;              program called list. This is currently only used for Unix
  572. ;;              dl (descriptive listings), when ange-ftp-dired-host-type
  573. ;;              is set to 'unix:dl.
  574.  
  575. ;; Bug report codes:
  576. ;;
  577. ;; Because of their naive faith in this code, there are certain situations
  578. ;; which the writers of this program believe could never happen. However,
  579. ;; being realists they have put calls to `error' in the program at these
  580. ;; points. These errors provide a code, which is an integer, greater than 1.
  581. ;; To aid debugging.  the error codes, and the functions in which they reside
  582. ;; are listed below.
  583. ;; 
  584. ;; 1: See ange-ftp-ls
  585. ;;
  586.  
  587. ;; -----------------------------------------------------------
  588. ;; Hall of fame:
  589. ;; -----------------------------------------------------------
  590. ;; 
  591. ;; Thanks to Roland McGrath for improving the filename syntax handling,
  592. ;; for suggesting many enhancements and for numerous cleanups to the code.
  593. ;;
  594. ;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways.
  595. ;;
  596. ;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and
  597. ;; dired / shell auto-loading.
  598. ;;
  599. ;; Thanks to Sebastian Kremer for dired support and for many ideas and
  600. ;; bugfixes.
  601. ;;
  602. ;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support,
  603. ;; VOS support, and hostname completion.
  604. ;;
  605. ;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help
  606. ;; with file-name expansion, efficiency worries, stylistic concerns and many
  607. ;; bugfixes.
  608. ;;
  609. ;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS,
  610. ;; MTS, CMS and UNIX-dls.  Sandy also added dired-support for non-UNIX OS and
  611. ;; auto-recognition of the host type.
  612. ;;
  613. ;; Thanks to Dave Smith who wrote the info file for ange-ftp.
  614. ;;
  615. ;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping
  616. ;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann,
  617. ;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill
  618. ;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay
  619. ;; Mathur, the folks on the ange-ftp-lovers mailing list and many others
  620. ;; whose names I've forgotten who have helped to debug and fix problems with
  621. ;; ange-ftp.el.
  622.  
  623. ;;; Code:
  624.  
  625. (require 'comint)
  626.  
  627. ;;;; ------------------------------------------------------------
  628. ;;;; User customization variables.
  629. ;;;; ------------------------------------------------------------
  630.  
  631. (defvar ange-ftp-name-format
  632.   '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*[^@/:.]\\):\\(.*\\)" . (3 2 4))
  633.   "*Format of a fully expanded remote file name.
  634. This is a list of the form \(REGEXP HOST USER NAME\),
  635. where REGEXP is a regular expression matching
  636. the full remote name, and HOST, USER, and NAME are the numbers of
  637. parenthesized expressions in REGEXP for the components (in that order).")
  638.  
  639. ;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of
  640. ;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs.
  641. ;; Otherwise, ange-ftp will go into multi-skip mode, and never come out.
  642.  
  643. (defvar ange-ftp-multi-msgs
  644.   "^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^331-\\|^4[25]1-\\|^530-"
  645.   "*Regular expression matching the start of a multiline ftp reply.")
  646.  
  647. (defvar ange-ftp-good-msgs
  648.   "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark"
  649.   "*Regular expression matching ftp \"success\" messages.")
  650.  
  651. ;; CMS and the odd VMS machine say 200 Port rather than 200 PORT.
  652. ;; Also CMS machines use a multiline 550- reply to say that you
  653. ;; don't have write permission. ange-ftp gets into multi-line skip
  654. ;; mode and hangs. Have it ignore 550- instead. It will then barf
  655. ;; when it gets the 550 line, as it should.
  656.  
  657. (defvar ange-ftp-skip-msgs
  658.   (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|"
  659.       "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|"
  660.       "^Data connection \\|"
  661.       "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye")
  662.   "*Regular expression matching ftp messages that can be ignored.")
  663.  
  664. (defvar ange-ftp-fatal-msgs
  665.   (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|"
  666.       "^No control connection\\|unknown host\\|^lost connection")
  667.   "*Regular expression matching ftp messages that indicate serious errors.
  668. These mean that the FTP process should (or already has) been killed.")
  669.  
  670. (defvar ange-ftp-gateway-fatal-msgs
  671.   "No route to host\\|Connection closed\\|No such host\\|Login incorrect"
  672.   "*Regular expression matching login failure messages from rlogin/telnet.")
  673.  
  674. (defvar ange-ftp-xfer-size-msgs
  675.   "^150 .* connection for .* (\\([0-9]+\\) bytes)"
  676.   "*Regular expression used to determine the number of bytes in a FTP transfer.")
  677.  
  678. (defvar ange-ftp-tmp-name-template "/tmp/ange-ftp"
  679.   "*Template used to create temporary files.")
  680.  
  681. (defvar ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp"
  682.   "*Template used to create temporary files when ftp-ing through a gateway.
  683. Files starting with this prefix need to be accessible from BOTH the local
  684. machine and the gateway machine, and need to have the SAME name on both
  685. machines, that is, /tmp is probably NOT what you want, since that is rarely
  686. cross-mounted.")
  687.  
  688. (defvar ange-ftp-netrc-filename "~/.netrc"
  689.   "*File in .netrc format to search for passwords.")
  690.  
  691. (defvar ange-ftp-disable-netrc-security-check nil
  692.   "*If non-nil avoid checking permissions on the .netrc file.")
  693.  
  694. (defvar ange-ftp-default-user nil
  695.   "*User name to use when none is specified in a file name.
  696. If non-nil but not a string, you are prompted for the name.
  697. If nil, the value of `ange-ftp-netrc-default-user' is used.
  698. If that is nil too, then your login name is used.
  699.  
  700. Once a connection to a given host has been initiated, the user name
  701. and password information for that host are cached and re-used by
  702. ange-ftp.  Use `ange-ftp-set-user' to change the cached values,
  703. since setting `ange-ftp-default-user' directly does not affect
  704. the cached information.")  
  705.  
  706. (defvar ange-ftp-netrc-default-user nil
  707.   "Alternate default user name to use when none is specified.
  708. This variable is set from the `default' command in your `.netrc' file,
  709. if there is one.")
  710.  
  711. (defvar ange-ftp-default-password nil
  712.   "*Password to use when the user name equals `ange-ftp-default-user'.")
  713.  
  714. (defvar ange-ftp-default-account nil
  715.   "*Account to use when the user name equals `ange-ftp-default-user'.")
  716.  
  717. (defvar ange-ftp-netrc-default-password nil
  718.   "*Password to use when the user name equals `ange-ftp-netrc-default-user'.")
  719.  
  720. (defvar ange-ftp-netrc-default-account nil
  721.   "*Account to use when the user name equals `ange-ftp-netrc-default-user'.")
  722.  
  723. (defvar ange-ftp-generate-anonymous-password t
  724.   "*If t, use value of `user-mail-address' as password for anonymous ftp.
  725. If a string, then use that string as the password.
  726. If nil, prompt the user for a password.")
  727.  
  728. (defvar ange-ftp-dumb-unix-host-regexp nil
  729.   "*If non-nil, regexp matching hosts on which `dir' command lists directory.")
  730.  
  731. (defvar ange-ftp-binary-file-name-regexp
  732.   (concat "\\.[zZ]$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|"
  733.       "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|"
  734.       "\\.EXE\\(;[0-9]+\\)?$\\|\\.[zZ]-part-..$\\|\\.gz$\\|"
  735.       "\\.taz$\\|\\.tgz$")
  736.   "*If a file matches this regexp then it is transferred in binary mode.")
  737.  
  738. (defvar ange-ftp-gateway-host nil
  739.   "*Name of host to use as gateway machine when local FTP isn't possible.")
  740.  
  741. (defvar ange-ftp-local-host-regexp ".*"
  742.   "*Regexp selecting hosts which can be reached directly with ftp.
  743. For other hosts the FTP process is started on \`ange-ftp-gateway-host\'
  744. instead, and/or reached via \`ange-ftp-gateway-ftp-program-name\'.")
  745.  
  746. (defvar ange-ftp-gateway-program-interactive nil
  747.   "*If non-nil then the gateway program should  give a shell prompt.
  748. Both telnet and rlogin do something like this.")
  749.  
  750. (defvar ange-ftp-gateway-program remote-shell-program
  751.   "*Name of program to spawn a shell on the gateway machine.
  752. Valid candidates are rsh (remsh on some systems), telnet and rlogin.  See
  753. also the gateway variable above.")
  754.  
  755. (defvar ange-ftp-gateway-prompt-pattern "^[^#$%>;\n]*[#$%>;] *"
  756.   "*Regexp matching prompt after complete login sequence on gateway machine.
  757. A match for this means the shell is now awaiting input.  Make this regexp as
  758. strict as possible; it shouldn't match *anything* at all except the user's
  759. initial prompt.  The above string will fail under most SUN-3's since it
  760. matches the login banner.")
  761.  
  762. (defvar ange-ftp-gateway-setup-term-command
  763.   (if (eq system-type 'hpux)
  764.       "stty -onlcr -echo\n"
  765.     "stty -echo nl\n")
  766.   "*Set up terminal after logging in to the gateway machine.
  767. This command should stop the terminal from echoing each command, and
  768. arrange to strip out trailing ^M characters.")
  769.  
  770. (defvar ange-ftp-smart-gateway nil
  771.   "*Non-nil means the ftp gateway and/or the gateway ftp program is smart.
  772. Don't bother telnetting, etc., already connected to desired host transparently,
  773. or just issue a user@host command in case \`ange-ftp-gateway-host\' is non-nil.")
  774.  
  775. (defvar ange-ftp-smart-gateway-port "21"
  776.   "*Port on gateway machine to use when smart gateway is in operation.")
  777.  
  778. (defvar ange-ftp-send-hash t
  779.   "*If non-nil, send the HASH command to the FTP client.")
  780.  
  781. (defvar ange-ftp-binary-hash-mark-size nil
  782.   "*Default size, in bytes, between hash-marks when transferring a binary file.
  783. If NIL, this variable will be locally overridden if the FTP client outputs a
  784. suitable response to the HASH command.  If non-NIL then this value takes
  785. precedence over the local value.")
  786.  
  787. (defvar ange-ftp-ascii-hash-mark-size 1024
  788.   "*Default size, in bytes, between hash-marks when transferring an ASCII file.
  789. This variable is buffer-local and will be locally overridden if the FTP client
  790. outputs a suitable response to the HASH command.")
  791.  
  792. (defvar ange-ftp-process-verbose t
  793.   "*If non-NIL then be chatty about interaction with the FTP process.")
  794.  
  795. (defvar ange-ftp-ftp-program-name "ftp"
  796.   "*Name of FTP program to run.")
  797.  
  798. (defvar ange-ftp-gateway-ftp-program-name "ftp"
  799.   "*Name of FTP program to run when accessing non-local hosts.
  800. Some AT&T folks claim to use something called `pftp' here.")
  801.  
  802. (defvar ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v")
  803.   "*A list of arguments passed to the FTP program when started.")
  804.  
  805. (defvar ange-ftp-nslookup-program nil
  806.   "*If non-NIL then a string naming nslookup program." )
  807.  
  808. (defvar ange-ftp-make-backup-files ()
  809.   "*Non-nil means make backup files for \"magic\" remote files.")
  810.  
  811. (defvar ange-ftp-retry-time 5
  812.   "*Number of seconds to wait before retry if file or listing doesn't arrive.
  813. This might need to be increased for very slow connections.")
  814.  
  815. (defvar ange-ftp-auto-save 0
  816.   "If 1, allows ange-ftp files to be auto-saved.
  817. If 0, suppresses auto-saving of ange-ftp files.
  818. Don't use any other value.")
  819.  
  820. ;;;; ------------------------------------------------------------
  821. ;;;; Hash table support.
  822. ;;;; ------------------------------------------------------------
  823.  
  824. (require 'backquote)
  825.  
  826. (defun ange-ftp-make-hashtable (&optional size)
  827.   "Make an obarray suitable for use as a hashtable.
  828. SIZE, if supplied, should be a prime number."
  829.   (make-vector (or size 31) 0))
  830.  
  831. (defun ange-ftp-map-hashtable (fun tbl)
  832.   "Call FUNCTION on each key and value in HASHTABLE."
  833.   (mapatoms
  834.    (function 
  835.     (lambda (sym)
  836.       (funcall fun (get sym 'key) (get sym 'val))))
  837.    tbl))
  838.  
  839. (defmacro ange-ftp-make-hash-key (key)
  840.   "Convert KEY into a suitable key for a hashtable."
  841.   (` (if (stringp (, key))
  842.      (, key)
  843.        (prin1-to-string (, key)))))
  844.  
  845. (defun ange-ftp-get-hash-entry (key tbl)
  846.   "Return the value associated with KEY in HASHTABLE."
  847.   (let ((sym (intern-soft (ange-ftp-make-hash-key key) tbl)))
  848.     (and sym (get sym 'val))))
  849.  
  850. (defun ange-ftp-put-hash-entry (key val tbl)
  851.   "Record an association between KEY and VALUE in HASHTABLE."
  852.   (let ((sym (intern (ange-ftp-make-hash-key key) tbl)))
  853.     (put sym 'val val)
  854.     (put sym 'key key)))
  855.  
  856. (defun ange-ftp-del-hash-entry (key tbl)
  857.   "Copy all symbols except KEY in HASHTABLE and return modified hashtable."
  858.   (let* ((len (length tbl))
  859.      (new-tbl (ange-ftp-make-hashtable len))
  860.      (i (1- len)))
  861.     (ange-ftp-map-hashtable
  862.      (function
  863.       (lambda (k v)
  864.     (or (equal k key)
  865.         (ange-ftp-put-hash-entry k v new-tbl))))
  866.      tbl)
  867.     (while (>= i 0)
  868.       (aset tbl i (aref new-tbl i))
  869.       (setq i (1- i)))
  870.     tbl))
  871.  
  872. (defun ange-ftp-hash-entry-exists-p (key tbl)
  873.   "Return whether there is an association for KEY in TABLE."
  874.   (intern-soft (ange-ftp-make-hash-key key) tbl))
  875.  
  876. (defun ange-ftp-hash-table-keys (tbl)
  877.   "Return a sorted list of all the active keys in TABLE, as strings."
  878.   (sort (all-completions "" tbl)
  879.     (function string-lessp)))
  880.  
  881. ;;;; ------------------------------------------------------------
  882. ;;;; Internal variables.
  883. ;;;; ------------------------------------------------------------
  884.  
  885. (defvar ange-ftp-data-buffer-name " *ftp data*"
  886.   "Buffer name to hold directory listing data received from ftp process.")
  887.  
  888. (defvar ange-ftp-netrc-modtime nil
  889.   "Last modified time of the netrc file from file-attributes.")
  890.  
  891. (defvar ange-ftp-user-hashtable (ange-ftp-make-hashtable)
  892.   "Hash table holding associations between HOST, USER pairs.")
  893.  
  894. (defvar ange-ftp-passwd-hashtable (ange-ftp-make-hashtable)
  895.   "Mapping between a HOST, USER pair and a PASSWORD for them.")
  896.  
  897. (defvar ange-ftp-account-hashtable (ange-ftp-make-hashtable)
  898.   "Mapping between a HOST, USER pair and a ACCOUNT password for them.")
  899.  
  900. (defvar ange-ftp-files-hashtable (ange-ftp-make-hashtable 97)
  901.   "Hash table for storing directories and their respective files.")
  902.  
  903. (defvar ange-ftp-ls-cache-lsargs nil
  904.   "Last set of args used by ange-ftp-ls.")
  905.  
  906. (defvar ange-ftp-ls-cache-file nil
  907.   "Last file passed to ange-ftp-ls.")
  908.  
  909. (defvar ange-ftp-ls-cache-res nil
  910.   "Last result returned from ange-ftp-ls.")
  911.  
  912. (defconst ange-ftp-expand-dir-hashtable (ange-ftp-make-hashtable))
  913.  
  914. (defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):")
  915.  
  916. ;; These are local variables in each FTP process buffer.
  917. (defvar ange-ftp-hash-mark-unit nil)
  918. (defvar ange-ftp-hash-mark-count nil)
  919. (defvar ange-ftp-xfer-size nil)
  920. (defvar ange-ftp-process-string nil)
  921. (defvar ange-ftp-process-result-line nil)
  922. (defvar ange-ftp-process-busy nil)
  923. (defvar ange-ftp-process-result nil)
  924. (defvar ange-ftp-process-multi-skip nil)
  925. (defvar ange-ftp-process-msg nil)
  926. (defvar ange-ftp-process-continue nil)
  927. (defvar ange-ftp-last-percent nil)
  928.  
  929. ;; These variables are bound by one function and examined by another.
  930. ;; Leave them void globally for error checking.
  931. (defvar ange-ftp-this-file)
  932. (defvar ange-ftp-this-dir)
  933. (defvar ange-ftp-this-user)
  934. (defvar ange-ftp-this-host)
  935. (defvar ange-ftp-this-msg)
  936. (defvar ange-ftp-completion-ignored-pattern)
  937. (defvar ange-ftp-trample-marker)
  938.  
  939. ;; New error symbols.
  940. (put 'ftp-error 'error-conditions '(ftp-error file-error error))
  941. ;; (put 'ftp-error 'error-message "FTP error")
  942.  
  943. ;;; ------------------------------------------------------------
  944. ;;; Enhanced message support.
  945. ;;; ------------------------------------------------------------
  946.  
  947. (defun ange-ftp-message (fmt &rest args)
  948.   "Display message in echo area, but indicate if truncated.
  949. Args are as in `message': a format string, plus arguments to be formatted."
  950.   (let ((msg (apply (function format) fmt args))
  951.     (max (window-width (minibuffer-window))))
  952.     (if noninteractive
  953.     msg
  954.       (if (>= (length msg) max)
  955.       ;; Take just the last MAX - 3 chars of the string.
  956.       (setq msg (concat "> " (substring msg (- 3 max)))))
  957.       (message "%s" msg))))
  958.  
  959. (defun ange-ftp-abbreviate-filename (file &optional new)
  960.   "Abbreviate the file name FILE relative to the default-directory.
  961. If the optional parameter NEW is given and the non-directory parts match,
  962. only return the directory part of FILE."
  963.   (save-match-data
  964.     (if (and default-directory
  965.          (string-match (concat "^"
  966.                    (regexp-quote default-directory)
  967.                    ".") file))
  968.     (setq file (substring file (1- (match-end 0)))))
  969.     (if (and new
  970.          (string-equal (file-name-nondirectory file)
  971.                (file-name-nondirectory new)))
  972.     (setq file (file-name-directory file)))
  973.     (or file "./")))
  974.  
  975. ;;;; ------------------------------------------------------------
  976. ;;;; User / Host mapping support.
  977. ;;;; ------------------------------------------------------------
  978.  
  979. (defun ange-ftp-set-user (host user)
  980.   "For a given HOST, set or change the default USER."
  981.   (interactive "sHost: \nsUser: ")
  982.   (ange-ftp-put-hash-entry host user ange-ftp-user-hashtable))
  983.  
  984. (defun ange-ftp-get-user (host)
  985.   "Given a HOST, return the default USER."
  986.   (ange-ftp-parse-netrc)
  987.   (let ((user (ange-ftp-get-hash-entry host ange-ftp-user-hashtable)))
  988.     (or user
  989.     (prog1
  990.         (setq user
  991.           (cond ((stringp ange-ftp-default-user)
  992.              ;; We have a default name.  Use it.
  993.              ange-ftp-default-user)
  994.             (ange-ftp-default-user
  995.              ;; Ask the user.
  996.              (let ((enable-recursive-minibuffers t))
  997.                (read-string (format "User for %s: " host)
  998.                     (user-login-name))))
  999.             (ange-ftp-netrc-default-user)
  1000.             ;; Default to the user's login name.
  1001.             (t
  1002.              (user-login-name))))
  1003.       (ange-ftp-set-user host user)))))
  1004.  
  1005. ;;;; ------------------------------------------------------------
  1006. ;;;; Password support.
  1007. ;;;; ------------------------------------------------------------
  1008.  
  1009. (defun ange-ftp-read-passwd (prompt &optional default)
  1010.   "Read a password, echoing `.' for each character typed.
  1011. End with RET, LFD, or ESC.  DEL or C-h rubs out.  C-u kills line.
  1012. Optional DEFAULT is password to start with."
  1013.   (let ((pass nil)
  1014.     (c 0)
  1015.     (echo-keystrokes 0)
  1016.     (cursor-in-echo-area t))
  1017.     (while (progn (message "%s%s"
  1018.                prompt
  1019.                (make-string (length pass) ?.))
  1020.           (setq c (read-char))
  1021.           (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
  1022.       (if (= c ?\C-u)
  1023.       (setq pass "")
  1024.     (if (and (/= c ?\b) (/= c ?\177))
  1025.         (setq pass (concat pass (char-to-string c)))
  1026.       (if (> (length pass) 0)
  1027.           (setq pass (substring pass 0 -1))))))
  1028.     (message "")
  1029.     (ange-ftp-repaint-minibuffer)
  1030.     (or pass default "")))
  1031.  
  1032. (defmacro ange-ftp-generate-passwd-key (host user)
  1033.   (` (concat (, host) "/" (, user))))
  1034.  
  1035. (defmacro ange-ftp-lookup-passwd (host user)
  1036.   (` (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key (, host) (, user))
  1037.                   ange-ftp-passwd-hashtable)))
  1038.  
  1039. (defun ange-ftp-set-passwd (host user passwd)
  1040.   "For a given HOST and USER, set or change the associated PASSWORD."
  1041.   (interactive (list (read-string "Host: ")
  1042.              (read-string "User: ")
  1043.              (ange-ftp-read-passwd "Password: ")))
  1044.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1045.                passwd
  1046.                ange-ftp-passwd-hashtable))
  1047.  
  1048. (defun ange-ftp-get-host-with-passwd (user)
  1049.   "Given a USER, return a host we know the password for."
  1050.   (ange-ftp-parse-netrc)
  1051.   (catch 'found-one
  1052.     (ange-ftp-map-hashtable
  1053.      (function (lambda (host val)
  1054.          (if (ange-ftp-lookup-passwd host user)
  1055.              (throw 'found-one host))))
  1056.      ange-ftp-user-hashtable)
  1057.     (save-match-data
  1058.       (ange-ftp-map-hashtable
  1059.        (function
  1060.     (lambda (key value)
  1061.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1062.           (let ((host (substring key 0 (match-beginning 1))))
  1063.         (if (and (string-equal user (substring key (match-end 1)))
  1064.              value)
  1065.             (throw 'found-one host))))))
  1066.        ange-ftp-passwd-hashtable))
  1067.     nil))
  1068.  
  1069. (defun ange-ftp-get-passwd (host user)
  1070.   "Return the password for specified HOST and USER, asking user if necessary."
  1071.   (ange-ftp-parse-netrc)
  1072.  
  1073.   ;; look up password in the hash table first; user might have overridden the
  1074.   ;; defaults.
  1075.   (cond ((ange-ftp-lookup-passwd host user))
  1076.     
  1077.     ;; See if default user and password set.
  1078.     ((and (stringp ange-ftp-default-user)
  1079.           ange-ftp-default-password
  1080.           (string-equal user ange-ftp-default-user))
  1081.      ange-ftp-default-password)
  1082.     
  1083.     ;; See if default user and password set from .netrc file.
  1084.     ((and (stringp ange-ftp-netrc-default-user)
  1085.           ange-ftp-netrc-default-password
  1086.           (string-equal user ange-ftp-netrc-default-user))
  1087.      ange-ftp-netrc-default-password)
  1088.     
  1089.     ;; anonymous ftp password is handled specially since there is an
  1090.     ;; unwritten rule about how that is used on the Internet.
  1091.     ((and (or (string-equal user "anonymous")
  1092.           (string-equal user "ftp"))
  1093.           ange-ftp-generate-anonymous-password)
  1094.      (if (stringp ange-ftp-generate-anonymous-password)
  1095.          ange-ftp-generate-anonymous-password
  1096.        user-mail-address))
  1097.     
  1098.     ;; see if same user has logged in to other hosts; if so then prompt
  1099.     ;; with the password that was used there.
  1100.     (t
  1101.      (let* ((other (ange-ftp-get-host-with-passwd user))
  1102.         (passwd (if other
  1103.                 
  1104.                 ;; found another machine with the same user.
  1105.                 ;; Try that account.
  1106.                 (ange-ftp-read-passwd
  1107.                  (format "passwd for %s@%s (default same as %s@%s): "
  1108.                      user host user other)
  1109.                  (ange-ftp-lookup-passwd other user))
  1110.               
  1111.               ;; I give up.  Ask the user for the password.
  1112.               (ange-ftp-read-passwd
  1113.                (format "Password for %s@%s: " user host)))))
  1114.        (ange-ftp-set-passwd host user passwd)
  1115.        passwd))))
  1116.  
  1117. ;;;; ------------------------------------------------------------
  1118. ;;;; Account support
  1119. ;;;; ------------------------------------------------------------
  1120.  
  1121. ;; Account passwords must be either specified in the .netrc file, or set
  1122. ;; manually by calling ange-ftp-set-account.  For the moment, ange-ftp doesn't
  1123. ;; check to see whether the FTP process is actually prompting for an account
  1124. ;; password.
  1125.  
  1126. (defun ange-ftp-set-account (host user account)
  1127.   "For a given HOST and USER, set or change the associated ACCOUNT password."
  1128.   (interactive (list (read-string "Host: ")
  1129.              (read-string "User: ")
  1130.              (ange-ftp-read-passwd "Account password: ")))
  1131.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1132.                account
  1133.                ange-ftp-account-hashtable))
  1134.  
  1135. (defun ange-ftp-get-account (host user)
  1136.   "Given a HOST and USER, return the FTP account."
  1137.   (ange-ftp-parse-netrc)
  1138.   (or (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key host user)
  1139.                    ange-ftp-account-hashtable)
  1140.       (and (stringp ange-ftp-default-user)
  1141.        (string-equal user ange-ftp-default-user)
  1142.        ange-ftp-default-account)
  1143.       (and (stringp ange-ftp-netrc-default-user)
  1144.        (string-equal user ange-ftp-netrc-default-user)
  1145.        ange-ftp-netrc-default-account)))
  1146.  
  1147. ;;;; ------------------------------------------------------------
  1148. ;;;; ~/.netrc support
  1149. ;;;; ------------------------------------------------------------
  1150.  
  1151. (defun ange-ftp-chase-symlinks (file)
  1152.   "Return the filename that FILE references, following all symbolic links."
  1153.   (let (temp)
  1154.     (while (setq temp (ange-ftp-real-file-symlink-p file))
  1155.       (setq file
  1156.         (if (file-name-absolute-p temp)
  1157.         temp
  1158.           (concat (file-name-directory file) temp)))))
  1159.   file)
  1160.  
  1161. ;; Move along current line looking for the value of the TOKEN.
  1162. ;; Valid separators between TOKEN and its value are commas and
  1163. ;; whitespace.  Second arg LIMIT is a limit for the search.
  1164.  
  1165. (defun ange-ftp-parse-netrc-token (token limit)
  1166.   (if (search-forward token limit t)
  1167.       (let (beg)
  1168.     (skip-chars-forward ", \t\r\n" limit)
  1169.     (if (eq (following-char) ?\")    ;quoted token value
  1170.         (progn (forward-char 1)
  1171.            (setq beg (point))
  1172.            (skip-chars-forward "^\"" limit)
  1173.            (forward-char 1)
  1174.            (buffer-substring beg (1- (point))))
  1175.       (setq beg (point))
  1176.       (skip-chars-forward "^, \t\r\n" limit)
  1177.       (buffer-substring beg (point))))))
  1178.  
  1179. ;; Extract the values for the tokens `machine', `login',
  1180. ;; `password' and `account' in the current buffer.  If successful,
  1181. ;; record the information found.
  1182.  
  1183. (defun ange-ftp-parse-netrc-group ()
  1184.   (let ((start (point))
  1185.     (end (save-excursion
  1186.            (if (looking-at "machine\\>")
  1187.            ;; Skip `machine' and the machine name that follows.
  1188.            (progn
  1189.              (skip-chars-forward "^ \t\n")
  1190.              (skip-chars-forward " \t\n")
  1191.              (skip-chars-forward "^ \t\n"))
  1192.          ;; Skip `default'.
  1193.          (skip-chars-forward "^ \t\n"))
  1194.            ;; Find start of the next `machine' or `default'
  1195.            ;; or the end of the buffer.
  1196.            (if (re-search-forward "machine\\>\\|default\\>" nil t)
  1197.            (match-beginning 0)
  1198.          (point-max))))
  1199.     machine login password account)
  1200.     (setq machine  (ange-ftp-parse-netrc-token "machine"  end)
  1201.       login    (ange-ftp-parse-netrc-token "login"    end)
  1202.       password (ange-ftp-parse-netrc-token "password" end)
  1203.       account  (ange-ftp-parse-netrc-token "account"  end))
  1204.     (if (and machine login)
  1205.     ;; found a `machine` token.
  1206.     (progn
  1207.       (ange-ftp-set-user machine login)
  1208.       (ange-ftp-set-passwd machine login password)
  1209.       (and account
  1210.            (ange-ftp-set-account machine login account)))
  1211.       (goto-char start)
  1212.       (if (search-forward "default" end t)
  1213.       ;; found a `default' token
  1214.       (progn
  1215.         (setq login    (ange-ftp-parse-netrc-token "login"    end)
  1216.           password (ange-ftp-parse-netrc-token "password" end)
  1217.           account  (ange-ftp-parse-netrc-token "account"  end))
  1218.         (and login
  1219.          (setq ange-ftp-netrc-default-user login))
  1220.         (and password
  1221.          (setq ange-ftp-netrc-default-password password))
  1222.         (and account
  1223.          (setq ange-ftp-netrc-default-account account)))))
  1224.     (goto-char end)))
  1225.  
  1226. ;; Read in ~/.netrc, if one exists.  If ~/.netrc file exists and has
  1227. ;; the correct permissions then extract the \`machine\', \`login\',
  1228. ;; \`password\' and \`account\' information from within.
  1229.  
  1230. (defun ange-ftp-parse-netrc ()
  1231.   ;; We set this before actually doing it to avoid the possibility
  1232.   ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file.
  1233.   (interactive)
  1234.   (let (file attr)
  1235.     (let ((default-directory "/"))
  1236.       (setq file (ange-ftp-chase-symlinks
  1237.           (ange-ftp-real-expand-file-name ange-ftp-netrc-filename)))
  1238.       (setq attr (ange-ftp-real-file-attributes file)))
  1239.     (if (and attr            ; file exists.
  1240.          (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))    ; file changed
  1241.     (save-match-data
  1242.       (if (or ange-ftp-disable-netrc-security-check
  1243.           (and (eq (nth 2 attr) (user-uid)) ; Same uids.
  1244.                (string-match ".r..------" (nth 8 attr))))
  1245.           (save-excursion
  1246.         ;; we are cheating a bit here.  I'm trying to do the equivalent
  1247.         ;; of find-file on the .netrc file, but then nuke it afterwards.
  1248.         ;; with the bit of logic below we should be able to have
  1249.         ;; encrypted .netrc files.
  1250.         (set-buffer (generate-new-buffer "*ftp-.netrc*"))
  1251.         (ange-ftp-real-insert-file-contents file)
  1252.         (setq buffer-file-name file)
  1253.         (setq default-directory (file-name-directory file))
  1254.         (normal-mode t)
  1255.         (mapcar 'funcall find-file-hooks)
  1256.         (setq buffer-file-name nil)
  1257.         (goto-char (point-min))
  1258.         (skip-chars-forward " \t\n")
  1259.         (while (not (eobp))
  1260.           (ange-ftp-parse-netrc-group))
  1261.         (kill-buffer (current-buffer)))
  1262.         (ange-ftp-message "%s either not owned by you or badly protected."
  1263.                   ange-ftp-netrc-filename)
  1264.         (sit-for 1))
  1265.       (setq ange-ftp-netrc-modtime (nth 5 attr))))))
  1266.  
  1267. ;; Return a list of prefixes of the form 'user@host:' to be used when
  1268. ;; completion is done in the root directory.
  1269.  
  1270. (defun ange-ftp-generate-root-prefixes ()
  1271.   (ange-ftp-parse-netrc)
  1272.   (save-match-data
  1273.     (let (res)
  1274.       (ange-ftp-map-hashtable
  1275.        (function
  1276.     (lambda (key value)
  1277.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1278.           (let ((host (substring key 0 (match-beginning 1)))
  1279.             (user (substring key (match-end 1))))
  1280.         (setq res (cons (list (concat user "@" host ":"))
  1281.                 res))))))
  1282.        ange-ftp-passwd-hashtable)
  1283.       (ange-ftp-map-hashtable
  1284.        (function (lambda (host user)
  1285.            (setq res (cons (list (concat host ":"))
  1286.                    res))))
  1287.        ange-ftp-user-hashtable)
  1288.       (or res (list nil)))))
  1289.  
  1290. ;;;; ------------------------------------------------------------
  1291. ;;;; Remote file name syntax support.
  1292. ;;;; ------------------------------------------------------------
  1293.  
  1294. (defmacro ange-ftp-ftp-name-component (n ns name)
  1295.   "Extract the Nth ftp file name component from NS."
  1296.   (` (let ((elt (nth (, n) (, ns))))
  1297.        (if (match-beginning elt)
  1298.        (substring (, name) (match-beginning elt) (match-end elt))))))
  1299.  
  1300. (defvar ange-ftp-ftp-name-arg "")
  1301. (defvar ange-ftp-ftp-name-res nil)
  1302.  
  1303. ;; Parse NAME according to `ange-ftp-name-format' (which see).
  1304. ;; Returns a list (HOST USER NAME), or nil if NAME does not match the format.
  1305. (defun ange-ftp-ftp-name (name)
  1306.   (if (string-equal name ange-ftp-ftp-name-arg)
  1307.       ange-ftp-ftp-name-res
  1308.     (setq ange-ftp-ftp-name-arg name
  1309.       ange-ftp-ftp-name-res
  1310.       (save-match-data
  1311.         (if (posix-string-match (car ange-ftp-name-format) name)
  1312.         (let* ((ns (cdr ange-ftp-name-format))
  1313.                (host (ange-ftp-ftp-name-component 0 ns name))
  1314.                (user (ange-ftp-ftp-name-component 1 ns name))
  1315.                (name (ange-ftp-ftp-name-component 2 ns name)))
  1316.           (if (zerop (length user))
  1317.               (setq user (ange-ftp-get-user host)))
  1318.           (list host user name))
  1319.           nil)))))
  1320.  
  1321. ;; Take a FULLNAME that matches according to ange-ftp-name-format and
  1322. ;; replace the name component with NAME.
  1323. (defun ange-ftp-replace-name-component (fullname name)
  1324.   (save-match-data
  1325.     (if (posix-string-match (car ange-ftp-name-format) fullname)
  1326.     (let* ((ns (cdr ange-ftp-name-format))
  1327.            (elt (nth 2 ns)))
  1328.       (concat (substring fullname 0 (match-beginning elt))
  1329.           name
  1330.           (substring fullname (match-end elt)))))))
  1331.  
  1332. ;;;; ------------------------------------------------------------
  1333. ;;;; Miscellaneous utils.
  1334. ;;;; ------------------------------------------------------------
  1335.  
  1336. ;; (setq ange-ftp-tmp-keymap (make-sparse-keymap))
  1337. ;; (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer)
  1338.  
  1339. (defun ange-ftp-repaint-minibuffer ()
  1340.   "Clear any existing minibuffer message; let the minibuffer contents show."
  1341.   (message nil))
  1342.  
  1343. ;; Return the name of the buffer that collects output from the ftp process
  1344. ;; connected to the given HOST and USER pair.
  1345. (defun ange-ftp-ftp-process-buffer (host user)
  1346.   (concat "*ftp " user "@" host "*"))
  1347.  
  1348. ;; Display the last chunk of output from the ftp process for the given HOST
  1349. ;; USER pair, and signal an error including MSG in the text.
  1350. (defun ange-ftp-error (host user msg)
  1351.   (let ((cur (selected-window))
  1352.     (pop-up-windows t))
  1353.     (pop-to-buffer
  1354.      (get-buffer-create
  1355.       (ange-ftp-ftp-process-buffer host user)))
  1356.     (goto-char (point-max))
  1357.     (select-window cur))
  1358.   (signal 'ftp-error (list (format "FTP Error: %s" msg))))
  1359.  
  1360. (defun ange-ftp-set-buffer-mode ()
  1361.   "Set correct modes for the current buffer if visiting a remote file."
  1362.   (if (and (stringp buffer-file-name)
  1363.        (ange-ftp-ftp-name buffer-file-name))
  1364.       (auto-save-mode ange-ftp-auto-save)))
  1365.  
  1366. (defun ange-ftp-kill-ftp-process (&optional buffer)
  1367.   "Kill the FTP process associated with BUFFER (the current buffer, if nil).
  1368. If the BUFFER's visited filename or default-directory is an ftp filename
  1369. then kill the related ftp process."
  1370.   (interactive "bKill FTP process associated with buffer: ")
  1371.   (if (null buffer)
  1372.       (setq buffer (current-buffer)))
  1373.   (let ((file (or (buffer-file-name buffer)
  1374.           (save-excursion (set-buffer buffer) default-directory))))
  1375.     (if file
  1376.     (let ((parsed (ange-ftp-ftp-name (expand-file-name file))))
  1377.       (if parsed
  1378.           (let ((host (nth 0 parsed))
  1379.             (user (nth 1 parsed)))
  1380.         (kill-buffer (ange-ftp-ftp-process-buffer host user))))))))
  1381.  
  1382. (defun ange-ftp-quote-string (string)
  1383.   "Quote any characters in STRING that may confuse the ftp process."
  1384.   (apply (function concat)
  1385.      (mapcar (function
  1386.            (lambda (char)
  1387.              (if (or (<= char ? )
  1388.                  (> char ?\~)
  1389.                      (= char ?\")
  1390.                  (= char ?\\))
  1391.              (vector ?\\ char)
  1392.                (vector char))))
  1393.          string)))
  1394.  
  1395. (defun ange-ftp-barf-if-not-directory (directory)
  1396.   (or (file-directory-p directory)
  1397.       (signal 'file-error
  1398.           (list "Opening directory"
  1399.             (if (file-exists-p directory)
  1400.             "not a directory"
  1401.               "no such file or directory")
  1402.             directory))))
  1403.  
  1404. ;;;; ------------------------------------------------------------
  1405. ;;;; FTP process filter support.
  1406. ;;;; ------------------------------------------------------------
  1407.  
  1408. (defun ange-ftp-process-handle-line (line proc)
  1409.   "Look at the given LINE from the ftp process PROC.
  1410. Try to categorize it into one of four categories:
  1411. good, skip, fatal, or unknown."
  1412.   (cond ((string-match ange-ftp-xfer-size-msgs line)
  1413.      (setq ange-ftp-xfer-size
  1414.            (ash (string-to-int (substring line
  1415.                           (match-beginning 1)
  1416.                           (match-end 1)))
  1417.             -10)))
  1418.     ((string-match ange-ftp-skip-msgs line)
  1419.      t)
  1420.     ((string-match ange-ftp-good-msgs line)
  1421.      (setq ange-ftp-process-busy nil
  1422.            ange-ftp-process-result t
  1423.            ange-ftp-process-result-line line))
  1424.     ;; Check this before checking for errors.
  1425.     ;; Otherwise the last line of these three seems to be an error:
  1426.     ;; 230-see a significant impact from the move.  For those of you who can't
  1427.     ;; 230-use DNS to resolve hostnames and get an error message like
  1428.     ;; 230-"ftp.stsci.edu: unknown host", the new IP address will be...
  1429.     ((string-match ange-ftp-multi-msgs line)
  1430.      (setq ange-ftp-process-multi-skip t))
  1431.     ((string-match ange-ftp-fatal-msgs line)
  1432.      (delete-process proc)
  1433.      (setq ange-ftp-process-busy nil
  1434.            ange-ftp-process-result-line line))
  1435.     (ange-ftp-process-multi-skip
  1436.      t)
  1437.     (t
  1438.      (setq ange-ftp-process-busy nil
  1439.            ange-ftp-process-result-line line))))
  1440.  
  1441. (defun ange-ftp-set-xfer-size (host user bytes)
  1442.   "Set the size of the next FTP transfer in bytes."
  1443.   (let ((proc (ange-ftp-get-process host user)))
  1444.     (if proc
  1445.     (let ((buf (process-buffer proc)))
  1446.       (if buf
  1447.           (save-excursion
  1448.         (set-buffer buf)
  1449.         (setq ange-ftp-xfer-size (ash bytes -10))))))))
  1450.  
  1451. (defun ange-ftp-process-handle-hash (str)
  1452.   "Remove hash marks from STRING and display count so far."
  1453.   (setq str (concat (substring str 0 (match-beginning 0))
  1454.             (substring str (match-end 0)))
  1455.     ange-ftp-hash-mark-count (+ (- (match-end 0)
  1456.                        (match-beginning 0))
  1457.                     ange-ftp-hash-mark-count))
  1458.   (and ange-ftp-hash-mark-unit
  1459.        ange-ftp-process-msg
  1460.        ange-ftp-process-verbose
  1461.        (not (eq (selected-window) (minibuffer-window)))
  1462.        (not (boundp 'search-message))    ;screws up isearch otherwise
  1463.        (not cursor-in-echo-area)    ;screws up y-or-n-p otherwise
  1464.        (let ((kbytes (ash (* ange-ftp-hash-mark-unit
  1465.                  ange-ftp-hash-mark-count)
  1466.               -6)))
  1467.        (if (zerop ange-ftp-xfer-size)
  1468.        (ange-ftp-message "%s...%dk" ange-ftp-process-msg kbytes)
  1469.      (let ((percent (/ (* 100 kbytes) ange-ftp-xfer-size)))
  1470.        ;; cut out the redisplay of identical %-age messages.
  1471.        (if (not (eq percent ange-ftp-last-percent))
  1472.            (progn
  1473.          (setq ange-ftp-last-percent percent)
  1474.          (ange-ftp-message "%s...%d%%" ange-ftp-process-msg percent)))))))
  1475.   str)
  1476.  
  1477. ;; Call the function specified by CONT.  CONT can be either a function
  1478. ;; or a list of a function and some args.  The first two parameters
  1479. ;; passed to the function will be RESULT and LINE.  The remaining args
  1480. ;; will be taken from CONT if a list was passed.
  1481.  
  1482. (defun ange-ftp-call-cont (cont result line)
  1483.   (if cont
  1484.       (if (and (listp cont)
  1485.            (not (eq (car cont) 'lambda)))
  1486.       (apply (car cont) result line (cdr cont))
  1487.     (funcall cont result line))))
  1488.  
  1489. ;; Build up a complete line of output from the ftp PROCESS and pass it
  1490. ;; on to ange-ftp-process-handle-line to deal with.
  1491.  
  1492. (defun ange-ftp-process-filter (proc str)
  1493.   (let ((buffer (process-buffer proc))
  1494.     (old-buffer (current-buffer)))
  1495.  
  1496.     ;; see if the buffer is still around... it could have been deleted.
  1497.     (if (buffer-name buffer)
  1498.     (unwind-protect
  1499.         (progn
  1500.           (set-buffer (process-buffer proc))
  1501.           
  1502.           ;; handle hash mark printing
  1503.           (and ange-ftp-process-busy
  1504.            (string-match "^#+$" str)
  1505.            (setq str (ange-ftp-process-handle-hash str)))
  1506.           (comint-output-filter proc str)
  1507.           ;; Replace STR by the result of the comint processing.
  1508.           (setq str (buffer-substring comint-last-output-start
  1509.                       (process-mark proc)))
  1510.           (if ange-ftp-process-busy
  1511.           (progn
  1512.             (setq ange-ftp-process-string (concat ange-ftp-process-string
  1513.                               str))
  1514.             
  1515.             ;; if we gave an empty password to the USER command earlier
  1516.             ;; then we should send a null password now.
  1517.             (if (string-match "Password: *$" ange-ftp-process-string)
  1518.             (send-string proc "\n"))))
  1519.           (while (and ange-ftp-process-busy
  1520.               (string-match "\n" ange-ftp-process-string))
  1521.         (let ((line (substring ange-ftp-process-string
  1522.                        0
  1523.                        (match-beginning 0))))
  1524.           (setq ange-ftp-process-string (substring ange-ftp-process-string
  1525.                                (match-end 0)))
  1526.           (while (string-match "^ftp> *" line)
  1527.             (setq line (substring line (match-end 0))))
  1528.           (ange-ftp-process-handle-line line proc)))
  1529.  
  1530.           ;; has the ftp client finished?  if so then do some clean-up
  1531.           ;; actions.
  1532.           (if (not ange-ftp-process-busy)
  1533.           (progn
  1534.             ;; reset the xfer size
  1535.             (setq ange-ftp-xfer-size 0)
  1536.  
  1537.             ;; issue the "done" message since we've finished.
  1538.             (if (and ange-ftp-process-msg
  1539.                  ange-ftp-process-verbose
  1540.                  ange-ftp-process-result)
  1541.             (progn
  1542.               (ange-ftp-message "%s...done" ange-ftp-process-msg)
  1543.               (ange-ftp-repaint-minibuffer)
  1544.               (setq ange-ftp-process-msg nil)))
  1545.             
  1546.             ;; is there a continuation we should be calling?  if so,
  1547.             ;; we'd better call it, making sure we only call it once.
  1548.             (if ange-ftp-process-continue
  1549.             (let ((cont ange-ftp-process-continue))
  1550.               (setq ange-ftp-process-continue nil)
  1551.               (ange-ftp-call-cont cont
  1552.                           ange-ftp-process-result
  1553.                           ange-ftp-process-result-line))))))
  1554.       (set-buffer old-buffer)))))
  1555.  
  1556. (defun ange-ftp-process-sentinel (proc str)
  1557.   "When ftp process changes state, nuke all file-entries in cache."
  1558.   (let ((name (process-name proc)))
  1559.     (if (string-match "\\*ftp \\([^@]+\\)@\\([^*]+\\)\\*" name)
  1560.     (let ((user (substring name (match-beginning 1) (match-end 1)))
  1561.           (host (substring name (match-beginning 2) (match-end 2))))
  1562.       (ange-ftp-wipe-file-entries host user))))
  1563.   (setq ange-ftp-ls-cache-file nil))
  1564.  
  1565. ;;;; ------------------------------------------------------------
  1566. ;;;; Gateway support.
  1567. ;;;; ------------------------------------------------------------
  1568.  
  1569. (defun ange-ftp-use-gateway-p (host)
  1570.   "Returns whether to access this host via a normal (non-smart) gateway."
  1571.   ;; yes, I know that I could simplify the following expression, but it is
  1572.   ;; clearer (to me at least) this way.
  1573.   (and (not ange-ftp-smart-gateway)
  1574.        (save-match-data
  1575.      (not (string-match ange-ftp-local-host-regexp host)))))
  1576.  
  1577. (defun ange-ftp-use-smart-gateway-p (host)
  1578.   "Returns whether to access this host via a smart gateway."
  1579.   (and ange-ftp-smart-gateway
  1580.        (save-match-data
  1581.      (not (string-match ange-ftp-local-host-regexp host)))))
  1582.  
  1583.  
  1584. ;;; ------------------------------------------------------------
  1585. ;;; Temporary file location and deletion...
  1586. ;;; ------------------------------------------------------------
  1587.  
  1588. (defvar ange-ftp-tmp-name-files ())
  1589. (defvar ange-ftp-tmp-name-hashtable (ange-ftp-make-hashtable 10))
  1590. (defvar ange-ftp-pid nil)
  1591.  
  1592. (defun ange-ftp-get-pid ()
  1593.   "Half-hearted attempt to get the current process's id."
  1594.   (setq ange-ftp-pid (substring (make-temp-name "") 1)))
  1595.  
  1596. (defun ange-ftp-make-tmp-name (host)
  1597.   "This routine will return the name of a new file."
  1598.   (let* ((template (if (ange-ftp-use-gateway-p host)
  1599.                ange-ftp-gateway-tmp-name-template
  1600.              ange-ftp-tmp-name-template))
  1601.      (pid (or ange-ftp-pid (ange-ftp-get-pid)))
  1602.      (start ?a)
  1603.      file entry)
  1604.     (while 
  1605.     (progn
  1606.       (setq file (format "%s%c%s" template start pid))
  1607.       (setq entry (intern file ange-ftp-tmp-name-hashtable))
  1608.       (or (memq entry ange-ftp-tmp-name-files)
  1609.           (ange-ftp-real-file-exists-p file)))
  1610.       (if (> (setq start (1+ start)) ?z)
  1611.       (progn
  1612.         (setq template (concat template "X"))
  1613.         (setq start ?a))))
  1614.     (setq ange-ftp-tmp-name-files
  1615.       (cons entry ange-ftp-tmp-name-files))
  1616.     file))
  1617.  
  1618. (defun ange-ftp-del-tmp-name (temp)
  1619.   (setq ange-ftp-tmp-name-files
  1620.     (delq (intern temp ange-ftp-tmp-name-hashtable)
  1621.           ange-ftp-tmp-name-files))
  1622.   (condition-case ()
  1623.       (ange-ftp-real-delete-file temp)
  1624.     (error nil)))
  1625.  
  1626. ;;;; ------------------------------------------------------------
  1627. ;;;; Interactive gateway program support.
  1628. ;;;; ------------------------------------------------------------
  1629.  
  1630. (defvar ange-ftp-gwp-running t)
  1631. (defvar ange-ftp-gwp-status nil)
  1632.  
  1633. (defun ange-ftp-gwp-sentinel (proc str)
  1634.   (setq ange-ftp-gwp-running nil))
  1635.  
  1636. (defun ange-ftp-gwp-filter (proc str)
  1637.   (comint-output-filter proc str)
  1638.   (save-excursion
  1639.     (set-buffer (process-buffer proc))
  1640.     ;; Replace STR by the result of the comint processing.
  1641.     (setq str (buffer-substring comint-last-output-start (process-mark proc))))
  1642.   (cond ((string-match "login: *$" str)
  1643.      (send-string proc
  1644.               (concat
  1645.                (let ((ange-ftp-default-user t))
  1646.              (ange-ftp-get-user ange-ftp-gateway-host))
  1647.                "\n")))
  1648.     ((string-match "Password: *$" str)
  1649.      (send-string proc
  1650.               (concat
  1651.                (ange-ftp-get-passwd ange-ftp-gateway-host
  1652.                         (ange-ftp-get-user
  1653.                          ange-ftp-gateway-host))
  1654.                "\n")))
  1655.     ((string-match ange-ftp-gateway-fatal-msgs str)
  1656.      (delete-process proc)
  1657.      (setq ange-ftp-gwp-running nil))
  1658.     ((string-match ange-ftp-gateway-prompt-pattern str)
  1659.      (setq ange-ftp-gwp-running nil
  1660.            ange-ftp-gwp-status t))))
  1661.  
  1662. (defun ange-ftp-gwp-start (host user name args)
  1663.   "Login to the gateway machine and fire up an ftp process."
  1664.   (let* ((gw-user (ange-ftp-get-user ange-ftp-gateway-host))
  1665.      ;; It would be nice to make process-connection-type nil,
  1666.      ;; but that doesn't work: ftp never responds.
  1667.      ;; Can anyone find a fix for that?
  1668.      (proc (let ((process-connection-type t))
  1669.          (start-process name name
  1670.                 ange-ftp-gateway-program
  1671.                 ange-ftp-gateway-host)))
  1672.      (ftp (mapconcat (function identity) args " ")))
  1673.     (process-kill-without-query proc)
  1674.     (set-process-sentinel proc (function ange-ftp-gwp-sentinel))
  1675.     (set-process-filter proc (function ange-ftp-gwp-filter))
  1676.     (save-excursion
  1677.       (set-buffer (process-buffer proc))
  1678.       (internal-ange-ftp-mode)
  1679.       (set-marker (process-mark proc) (point)))
  1680.     (setq ange-ftp-gwp-running t
  1681.       ange-ftp-gwp-status nil)
  1682.     (ange-ftp-message "Connecting to gateway %s..." ange-ftp-gateway-host)
  1683.     (while ange-ftp-gwp-running        ;perform login sequence
  1684.       (accept-process-output proc))
  1685.     (if (not ange-ftp-gwp-status)
  1686.     (ange-ftp-error host user "unable to login to gateway"))
  1687.     (ange-ftp-message "Connecting to gateway %s...done" ange-ftp-gateway-host)
  1688.     (setq ange-ftp-gwp-running t
  1689.       ange-ftp-gwp-status nil)
  1690.     (process-send-string proc ange-ftp-gateway-setup-term-command)
  1691.     (while ange-ftp-gwp-running        ;zap ^M's and double echoing.
  1692.       (accept-process-output proc))
  1693.     (if (not ange-ftp-gwp-status)
  1694.     (ange-ftp-error host user "unable to set terminal modes on gateway"))
  1695.     (setq ange-ftp-gwp-running t
  1696.       ange-ftp-gwp-status nil)
  1697.     (process-send-string proc (concat "exec " ftp "\n")) ;spawn ftp process
  1698.     proc))
  1699.  
  1700. ;;;; ------------------------------------------------------------
  1701. ;;;; Support for sending commands to the ftp process.
  1702. ;;;; ------------------------------------------------------------
  1703.  
  1704. (defun ange-ftp-raw-send-cmd (proc cmd &optional msg cont nowait)
  1705.   "Low-level routine to send the given ftp CMD to the ftp PROCESS.
  1706. MSG is an optional message to output before and after the command.
  1707. If CONT is non-NIL then it is either a function or a list of function and
  1708. some arguments.  The function will be called when the ftp command has completed.
  1709. If CONT is NIL then this routine will return \( RESULT . LINE \) where RESULT
  1710. is whether the command was successful, and LINE is the line from the FTP
  1711. process that caused the command to complete.
  1712. If NOWAIT is given then the routine will return immediately the command has
  1713. been queued with no result.  CONT will still be called, however."
  1714.   (if (memq (process-status proc) '(run open))
  1715.       (save-excursion
  1716.     (set-buffer (process-buffer proc))
  1717.     (ange-ftp-wait-not-busy proc)
  1718.     (setq ange-ftp-process-string ""
  1719.           ange-ftp-process-result-line ""
  1720.           ange-ftp-process-busy t
  1721.           ange-ftp-process-result nil
  1722.           ange-ftp-process-multi-skip nil
  1723.           ange-ftp-process-msg msg
  1724.           ange-ftp-process-continue cont
  1725.           ange-ftp-hash-mark-count 0
  1726.           ange-ftp-last-percent -1
  1727.           cmd (concat cmd "\n"))
  1728.     (and msg ange-ftp-process-verbose (ange-ftp-message "%s..." msg))
  1729.     (goto-char (point-max))
  1730.     (move-marker comint-last-input-start (point))
  1731.     ;; don't insert the password into the buffer on the USER command.
  1732.     (save-match-data
  1733.       (if (string-match "^user \"[^\"]*\"" cmd)
  1734.           (insert (substring cmd 0 (match-end 0)) " Turtle Power!\n")
  1735.         (insert cmd)))
  1736.     (move-marker comint-last-input-end (point))
  1737.     (send-string proc cmd)
  1738.     (set-marker (process-mark proc) (point))
  1739.     (if nowait
  1740.         nil
  1741.       (ange-ftp-wait-not-busy proc)
  1742.       (if cont
  1743.           nil            ;cont has already been called
  1744.         (cons ange-ftp-process-result ange-ftp-process-result-line))))))
  1745.  
  1746. ;; Wait for the ange-ftp process PROC not to be busy.
  1747. (defun ange-ftp-wait-not-busy (proc)
  1748.   (save-excursion
  1749.     (set-buffer (process-buffer proc))
  1750.     (condition-case nil
  1751.     ;; This is a kludge to let user quit in case ftp gets hung.
  1752.     ;; It matters because this function can be called from the filter.
  1753.     ;; It is bad to allow quitting in a filter, but getting hung
  1754.     ;; is worse.  By binding quit-flag to nil, we might avoid
  1755.     ;; most of the probability of getting screwed because the user
  1756.     ;; wants to quit some command.
  1757.     (let ((quit-flag nil)
  1758.           (inhibit-quit nil))
  1759.       (while ange-ftp-process-busy
  1760.         (accept-process-output proc)))
  1761.       (quit
  1762.        ;; If the user does quit out of this,
  1763.        ;; kill the process.  That stops any transfer in progress.
  1764.        ;; The next operation will open a new ftp connection.
  1765.        (delete-process proc)
  1766.        (signal 'quit nil)))))
  1767.  
  1768. (defun ange-ftp-nslookup-host (host)
  1769.   "Attempt to resolve the given HOSTNAME using nslookup if possible."
  1770.   (interactive "sHost:  ")
  1771.   (if ange-ftp-nslookup-program
  1772.       (let ((default-directory
  1773.           (if (file-accessible-directory-p default-directory)
  1774.           default-directory
  1775.         exec-directory))
  1776.         ;; It would be nice to make process-connection-type nil,
  1777.         ;; but that doesn't work: ftp never responds.
  1778.         ;; Can anyone find a fix for that?
  1779.         (proc (let ((process-connection-type t))
  1780.             (start-process " *nslookup*" " *nslookup*"
  1781.                    ange-ftp-nslookup-program host)))
  1782.         (res host))
  1783.     (process-kill-without-query proc)
  1784.     (save-excursion
  1785.       (set-buffer (process-buffer proc))
  1786.       (while (memq (process-status proc) '(run open))
  1787.         (accept-process-output proc))
  1788.       (goto-char (point-min))
  1789.       (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t)
  1790.           (setq res (buffer-substring (match-beginning 1)
  1791.                       (match-end 1))))
  1792.       (kill-buffer (current-buffer)))
  1793.     res)
  1794.     host))
  1795.  
  1796. (defun ange-ftp-start-process (host user name)
  1797.   "Spawn a new ftp process ready to connect to machine HOST and give it NAME.
  1798. If HOST is only ftp-able through a gateway machine then spawn a shell
  1799. on the gateway machine to do the ftp instead."
  1800.   (let* ((use-gateway (ange-ftp-use-gateway-p host))
  1801.      (use-smart-ftp (and (not ange-ftp-gateway-host)
  1802.                  (ange-ftp-use-smart-gateway-p host)))
  1803.      (ftp-prog (if (or use-gateway
  1804.                use-smart-ftp) 
  1805.                ange-ftp-gateway-ftp-program-name
  1806.              ange-ftp-ftp-program-name))
  1807.      (args (append (list ftp-prog) ange-ftp-ftp-program-args))
  1808.      ;; Without the following binding, ange-ftp-start-process
  1809.      ;; recurses on file-accessible-directory-p, since it needs to
  1810.      ;; restart its process in order to determine anything about
  1811.      ;; default-directory.
  1812.      (file-name-handler-alist)
  1813.      (default-directory
  1814.        (if (file-accessible-directory-p default-directory)
  1815.            default-directory
  1816.          exec-directory))
  1817.      proc)
  1818.     ;; It would be nice to make process-connection-type nil,
  1819.     ;; but that doesn't work: ftp never responds.
  1820.     ;; Can anyone find a fix for that?
  1821.     (let ((process-connection-type t)
  1822.       (process-environment process-environment))
  1823.       ;; This tells GNU ftp not to output any fancy escape sequences.
  1824.       (setenv "TERM" "dumb")
  1825.       (if use-gateway
  1826.       (if ange-ftp-gateway-program-interactive
  1827.           (setq proc (ange-ftp-gwp-start host user name args))
  1828.         (setq proc (apply 'start-process name name
  1829.                   (append (list ange-ftp-gateway-program
  1830.                         ange-ftp-gateway-host)
  1831.                       args))))
  1832.     (setq proc (apply 'start-process name name args))))
  1833.     (process-kill-without-query proc)
  1834.     (save-excursion
  1835.       (set-buffer (process-buffer proc))
  1836.       (internal-ange-ftp-mode))
  1837.     (set-process-sentinel proc (function ange-ftp-process-sentinel))
  1838.     (set-process-filter proc (function ange-ftp-process-filter))
  1839.     (accept-process-output proc)    ;wait for ftp startup message
  1840.     proc))
  1841.  
  1842. (defun internal-ange-ftp-mode ()
  1843.   "Major mode for interacting with the FTP process.
  1844.  
  1845. \\{comint-mode-map}"
  1846.   (interactive)
  1847.   (comint-mode)
  1848.   (setq major-mode 'internal-ange-ftp-mode)
  1849.   (setq mode-name "Internal Ange-ftp")
  1850.   (let ((proc (get-buffer-process (current-buffer))))
  1851.     (goto-char (point-max))
  1852.     (set-marker (process-mark proc) (point))
  1853.     (make-local-variable 'ange-ftp-process-string)
  1854.     (setq ange-ftp-process-string "")
  1855.     (make-local-variable 'ange-ftp-process-busy)
  1856.     (make-local-variable 'ange-ftp-process-result)
  1857.     (make-local-variable 'ange-ftp-process-msg)
  1858.     (make-local-variable 'ange-ftp-process-multi-skip)
  1859.     (make-local-variable 'ange-ftp-process-result-line)
  1860.     (make-local-variable 'ange-ftp-process-continue)
  1861.     (make-local-variable 'ange-ftp-hash-mark-count)
  1862.     (make-local-variable 'ange-ftp-binary-hash-mark-size)
  1863.     (make-local-variable 'ange-ftp-ascii-hash-mark-size)
  1864.     (make-local-variable 'ange-ftp-hash-mark-unit)
  1865.     (make-local-variable 'ange-ftp-xfer-size)
  1866.     (make-local-variable 'ange-ftp-last-percent)
  1867.     (setq ange-ftp-hash-mark-count 0)
  1868.     (setq ange-ftp-xfer-size 0)
  1869.     (setq ange-ftp-process-result-line "")
  1870.  
  1871.     (setq comint-prompt-regexp "^ftp> ")
  1872.     (make-local-variable 'comint-password-prompt-regexp)
  1873.     ;; This is a regexp that can't match anything.
  1874.     ;; ange-ftp has its own ways of handling passwords.
  1875.     (setq comint-password-prompt-regexp "^a\\'z")
  1876.     (make-local-variable 'paragraph-start)
  1877.     (setq paragraph-start comint-prompt-regexp)))
  1878.  
  1879. (defun ange-ftp-smart-login (host user pass account proc)
  1880.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1881. PROC is the FTP-client's process.  This routine uses the smart-gateway
  1882. host specified in ``ange-ftp-gateway-host''."
  1883.   (let ((result (ange-ftp-raw-send-cmd
  1884.          proc
  1885.          (format "open %s %s"
  1886.              (ange-ftp-nslookup-host ange-ftp-gateway-host)
  1887.              ange-ftp-smart-gateway-port)
  1888.          (format "Opening FTP connection to %s via %s"
  1889.              host
  1890.              ange-ftp-gateway-host))))
  1891.     (or (car result)
  1892.     (ange-ftp-error host user 
  1893.             (concat "OPEN request failed: "
  1894.                 (cdr result))))
  1895.     (setq result (ange-ftp-raw-send-cmd
  1896.           proc (format "user \"%s\"@%s %s %s"
  1897.                    user
  1898.                    (ange-ftp-nslookup-host host)
  1899.                    pass
  1900.                    account)
  1901.           (format "Logging in as user %s@%s"
  1902.               user host)))
  1903.     (or (car result)
  1904.     (progn
  1905.       (ange-ftp-set-passwd host user nil) ; reset password
  1906.       (ange-ftp-set-account host user nil) ; reset account
  1907.       (ange-ftp-error host user
  1908.               (concat "USER request failed: "
  1909.                   (cdr result)))))))
  1910.  
  1911. (defun ange-ftp-normal-login (host user pass account proc)
  1912.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1913. PROC is the process to the FTP-client."
  1914.   (let* ((nshost (ange-ftp-nslookup-host host))
  1915.      (result (ange-ftp-raw-send-cmd
  1916.          proc
  1917.          (format "open %s" nshost)
  1918.          (format "Opening FTP connection to %s" host))))
  1919.     (or (car result)
  1920.     (ange-ftp-error host user
  1921.             (concat "OPEN request failed: "
  1922.                 (cdr result))))
  1923.     (setq result (ange-ftp-raw-send-cmd
  1924.           proc
  1925.           (if (ange-ftp-use-smart-gateway-p host)
  1926.               (format "user \"%s\"@%s %s %s" user nshost pass account)
  1927.             (format "user \"%s\" %s %s" user pass account))
  1928.           (format "Logging in as user %s@%s" user host)))
  1929.     (or (car result)
  1930.     (progn
  1931.       (ange-ftp-set-passwd host user nil) ;reset password.
  1932.       (ange-ftp-set-account host user nil) ;reset account.
  1933.       (ange-ftp-error host user
  1934.               (concat "USER request failed: "
  1935.                   (cdr result)))))))
  1936.  
  1937. ;; ange@hplb.hpl.hp.com says this should not be changed.
  1938. (defvar ange-ftp-hash-mark-msgs
  1939.   "[hH]ash mark [^0-9]*\\([0-9]+\\)"
  1940.   "*Regexp matching the FTP client's output upon doing a HASH command.")
  1941.  
  1942. (defun ange-ftp-guess-hash-mark-size (proc)
  1943.   (if ange-ftp-send-hash
  1944.       (save-excursion
  1945.     (set-buffer (process-buffer proc))
  1946.     (let* ((status (ange-ftp-raw-send-cmd proc "hash"))
  1947.            (result (car status))
  1948.            (line (cdr status)))
  1949.       (save-match-data
  1950.         (if (string-match ange-ftp-hash-mark-msgs line)
  1951.         (let ((size (string-to-int
  1952.                 (substring line
  1953.                        (match-beginning 1)
  1954.                        (match-end 1)))))
  1955.           (setq ange-ftp-ascii-hash-mark-size size
  1956.             ange-ftp-hash-mark-unit (ash size -4))
  1957.  
  1958.           ;; if a default value for this is set, use that value.
  1959.           (or ange-ftp-binary-hash-mark-size
  1960.               (setq ange-ftp-binary-hash-mark-size size)))))))))
  1961.  
  1962. (defun ange-ftp-get-process (host user)
  1963.   "Return an FTP subprocess connected to HOST and logged in as USER.
  1964. Create a new process if needed."
  1965.   (let* ((name (ange-ftp-ftp-process-buffer host user))
  1966.      (proc (get-process name)))
  1967.     (if (and proc (memq (process-status proc) '(run open)))
  1968.     proc
  1969.       (let ((pass (ange-ftp-quote-string
  1970.            (ange-ftp-get-passwd host user)))
  1971.         (account (ange-ftp-quote-string
  1972.               (ange-ftp-get-account host user))))
  1973.     ;; grab a suitable process.
  1974.     (setq proc (ange-ftp-start-process host user name))
  1975.     
  1976.     ;; login to FTP server.
  1977.     (if (and (ange-ftp-use-smart-gateway-p host)
  1978.          ange-ftp-gateway-host)
  1979.         (ange-ftp-smart-login host user pass account proc)
  1980.       (ange-ftp-normal-login host user pass account proc))
  1981.       
  1982.     ;; Tell client to send back hash-marks as progress.  It isn't usually
  1983.     ;; fatal if this command fails.
  1984.     (ange-ftp-guess-hash-mark-size proc)
  1985.  
  1986.     ;; Guess at the host type.
  1987.     (ange-ftp-guess-host-type host user)
  1988.  
  1989.     ;; Run any user-specified hooks.  Note that proc, host and user are
  1990.     ;; dynamically bound at this point.
  1991.     (run-hooks 'ange-ftp-process-startup-hook))
  1992.       proc)))
  1993.  
  1994. ;; Variables for caching host and host-type
  1995. (defvar ange-ftp-host-cache nil)
  1996. (defvar ange-ftp-host-type-cache nil)
  1997.  
  1998. ;; If ange-ftp-host-type is called with the optional user
  1999. ;; argument, it will attempt to guess the host type by connecting
  2000. ;; as user, if necessary. For efficiency, I have tried to give this
  2001. ;; optional second argument only when necessary. Have I missed any calls
  2002. ;; to ange-ftp-host-type where it should have been supplied?
  2003.  
  2004. (defun ange-ftp-host-type (host &optional user)
  2005.   "Return a symbol which represents the type of the HOST given.
  2006. If the optional argument USER is given, attempts to guess the
  2007. host-type by logging in as USER."
  2008.   (if (eq host ange-ftp-host-cache)
  2009.       ange-ftp-host-type-cache
  2010.     ;; Trigger an ftp connection, in case we need to guess at the host type.
  2011.     (if (and user (ange-ftp-get-process host user) (eq host ange-ftp-host-cache))
  2012.     ange-ftp-host-type-cache
  2013.       (setq ange-ftp-host-cache host
  2014.         ange-ftp-host-type-cache
  2015.         (cond ((ange-ftp-dumb-unix-host host)
  2016.            'dumb-unix)
  2017. ;;          ((and (fboundp 'ange-ftp-vos-host)
  2018. ;;            (ange-ftp-vos-host host))
  2019. ;;           'vos)
  2020.           ((and (fboundp 'ange-ftp-vms-host)
  2021.             (ange-ftp-vms-host host))
  2022.            'vms)
  2023.           ((and (fboundp 'ange-ftp-mts-host)
  2024.             (ange-ftp-mts-host host))
  2025.            'mts)
  2026.           ((and (fboundp 'ange-ftp-cms-host)
  2027.             (ange-ftp-cms-host host))
  2028.            'cms)
  2029.           (t
  2030.            'unix))))))
  2031.  
  2032. ;; It would be nice to abstract the functions ange-ftp-TYPE-host and
  2033. ;; ange-ftp-add-TYPE-host. The trick is to abstract these functions
  2034. ;; without sacrificing speed. Also, having separate variables
  2035. ;; ange-ftp-TYPE-regexp is more user friendly then requiring the user to
  2036. ;; set an alist to indicate that a host is of a given type. Even with
  2037. ;; automatic host type recognition, setting a regexp is still a good idea
  2038. ;; (for efficiency) if you log into a particular non-UNIX host frequently.
  2039.  
  2040. (defvar ange-ftp-fix-name-func-alist nil
  2041.   "Alist saying how to convert file name to the host's syntax.
  2042. Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  2043. which can change a UNIX file name into a name more suitable for a host of type
  2044. TYPE.")
  2045.  
  2046. (defvar ange-ftp-fix-dir-name-func-alist nil
  2047.   "Alist saying how to convert directory name to the host's syntax.
  2048. Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  2049. which can change UNIX directory name into a directory name more suitable
  2050. for a host of type TYPE.")
  2051.  
  2052. ;; *** Perhaps the sense of this variable should be inverted, since there
  2053. ;; *** is only 1 host type that can take ls-style listing options.
  2054. (defvar ange-ftp-dumb-host-types '(dumb-unix)
  2055.   "List of host types that can't take UNIX ls-style listing options.")
  2056.  
  2057. (defun ange-ftp-send-cmd (host user cmd &optional msg cont nowait)
  2058.   "Find an ftp process connected to HOST logged in as USER and send it CMD.
  2059. MSG is an optional status message to be output before and after issuing the
  2060. command.
  2061. See the documentation for ange-ftp-raw-send-cmd for a description of CONT
  2062. and NOWAIT."
  2063.   ;; Handle conversion to remote file name syntax and remote ls option
  2064.   ;; capability.
  2065.   (let ((cmd0 (car cmd))
  2066.     (cmd1 (nth 1 cmd))
  2067.     (ange-ftp-this-user user)
  2068.     (ange-ftp-this-host host)
  2069.     (ange-ftp-this-msg msg)
  2070.     cmd2 cmd3 host-type fix-name-func)
  2071.  
  2072.     (cond
  2073.      
  2074.      ;; pwd case (We don't care what host-type.)
  2075.      ((null cmd1))
  2076.      
  2077.      ;; cmd == 'dir "remote-name" "local-name" "ls-switches"
  2078.      ((progn
  2079.        (setq cmd2 (nth 2 cmd)
  2080.          host-type (ange-ftp-host-type host user))
  2081.        ;; This will trigger an FTP login, if one doesn't exist
  2082.        (eq cmd0 'dir))
  2083.       (setq cmd1 (funcall
  2084.           (or (cdr (assq host-type ange-ftp-fix-dir-name-func-alist))
  2085.               'identity)
  2086.           cmd1)
  2087.         cmd3 (nth 3 cmd))
  2088.       ;; Need to deal with the HP-UX ftp bug. This should also allow
  2089.       ;; us to resolve symlinks to directories on SysV machines. (Sebastian will
  2090.       ;; be happy.)
  2091.       (and (eq host-type 'unix)
  2092.        (string-match "/$" cmd1)
  2093.        (not (string-match "R" cmd3))
  2094.        (setq cmd1 (concat cmd1 ".")))
  2095.       ;; If the remote ls can take switches, put them in
  2096.       (or (memq host-type ange-ftp-dumb-host-types)
  2097.       (setq cmd0 'ls
  2098.         cmd1 (format "\"%s %s\"" cmd3 cmd1))))
  2099.      
  2100.      ;; First argument is the remote name
  2101.      ((progn
  2102.     (setq fix-name-func (or (cdr (assq host-type
  2103.                        ange-ftp-fix-name-func-alist))
  2104.                 'identity))
  2105.     (memq cmd0 '(get delete mkdir rmdir cd)))
  2106.       (setq cmd1 (funcall fix-name-func cmd1)))
  2107.  
  2108.      ;; Second argument is the remote name
  2109.      ((memq cmd0 '(append put chmod))
  2110.       (setq cmd2 (funcall fix-name-func cmd2)))
  2111.  
  2112.      ;; Both arguments are remote names
  2113.      ((eq cmd0 'rename)
  2114.       (setq cmd1 (funcall fix-name-func cmd1)
  2115.         cmd2 (funcall fix-name-func cmd2))))
  2116.       
  2117.     ;; Turn the command into one long string 
  2118.     (setq cmd0 (symbol-name cmd0))
  2119.     (setq cmd (concat cmd0
  2120.               (and cmd1 (concat " " cmd1))
  2121.               (and cmd2 (concat " " cmd2))))
  2122.  
  2123.     ;; Actually send the resulting command.
  2124.     (let (afsc-result
  2125.       afsc-line)
  2126.       (ange-ftp-raw-send-cmd
  2127.        (ange-ftp-get-process host user)
  2128.        cmd
  2129.        msg
  2130.        (list
  2131.     (function (lambda (result line host user
  2132.                   cmd msg cont nowait)
  2133.             (or cont
  2134.             (setq afsc-result result
  2135.                   afsc-line line))
  2136.             (if result
  2137.             (ange-ftp-call-cont cont result line)
  2138.               (ange-ftp-raw-send-cmd
  2139.                (ange-ftp-get-process host user)
  2140.                cmd
  2141.                msg
  2142.                (list
  2143.             (function (lambda (result line cont)
  2144.                     (or cont
  2145.                     (setq afsc-result result
  2146.                           afsc-line line))
  2147.                     (ange-ftp-call-cont cont result line)))
  2148.             cont)
  2149.                nowait))))
  2150.     host user cmd msg cont nowait)
  2151.        nowait)
  2152.  
  2153.       (if nowait
  2154.       nil
  2155.     (if cont
  2156.         nil
  2157.       (cons afsc-result afsc-line))))))
  2158.  
  2159. ;; It might be nice to message users about the host type identified,
  2160. ;; but there is so much other messaging going on, it would not be
  2161. ;; seen. No point in slowing things down just so users can read
  2162. ;; a host type message.
  2163.  
  2164. (defconst ange-ftp-cms-name-template
  2165.   (concat
  2166.    "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
  2167.    "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$"))
  2168. (defconst ange-ftp-vms-name-template
  2169.   "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
  2170. (defconst ange-ftp-mts-name-template
  2171.   "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
  2172.  
  2173. (defun ange-ftp-guess-host-type (host user)
  2174.   "Guess at the the host type of HOST.
  2175. Works by doing a pwd and examining the directory syntax."
  2176.   (let ((host-type (ange-ftp-host-type host))
  2177.     (key (concat host "/" user "/~")))
  2178.     (if (eq host-type 'unix)
  2179.     ;; Note that ange-ftp-host-type returns unix as the default value.
  2180.     (save-match-data
  2181.       (let* ((result (ange-ftp-get-pwd host user))
  2182.          (dir (car result))
  2183.          fix-name-func)
  2184.         (cond ((null dir)
  2185.            (message "Warning! Unable to get home directory")
  2186.            (sit-for 1)
  2187.            (if (string-match
  2188.             "^450 No current working directory defined$"
  2189.             (cdr result))
  2190.                
  2191.                ;; We'll assume that if pwd bombs with this
  2192.                ;; error message, then it's CMS.
  2193.                (progn
  2194.              (ange-ftp-add-cms-host host)
  2195.              (setq ange-ftp-host-cache host
  2196.                    ange-ftp-host-type-cache 'cms))))
  2197.  
  2198.           ;; try for VMS
  2199.           ((string-match ange-ftp-vms-name-template dir)
  2200.            (ange-ftp-add-vms-host host)
  2201.            ;; The add-host functions clear the host type cache.
  2202.            ;; Therefore, need to set the cache afterwards.
  2203.            (setq ange-ftp-host-cache host
  2204.              ange-ftp-host-type-cache 'vms))
  2205.  
  2206.           ;; try for MTS
  2207.           ((string-match ange-ftp-mts-name-template dir)
  2208.            (ange-ftp-add-mts-host host)
  2209.            (setq ange-ftp-host-cache host
  2210.              ange-ftp-host-type-cache 'mts))
  2211.           
  2212.           ;; try for CMS
  2213.           ((string-match ange-ftp-cms-name-template dir)
  2214.            (ange-ftp-add-cms-host host)
  2215.            (setq ange-ftp-host-cache host
  2216.              ange-ftp-host-type-cache 'cms))
  2217.  
  2218.           ;; assume UN*X
  2219.           (t
  2220.            (setq ange-ftp-host-cache host
  2221.              ange-ftp-host-type-cache 'unix)))
  2222.  
  2223.         ;; Now that we have done a pwd, might as well put it in
  2224.         ;; the expand-dir hashtable.
  2225.         (let ((ange-ftp-this-user user)
  2226.           (ange-ftp-this-host host))
  2227.           (setq fix-name-func (cdr (assq ange-ftp-host-type-cache
  2228.                          ange-ftp-fix-name-func-alist)))
  2229.           (if fix-name-func
  2230.           (setq dir (funcall fix-name-func dir 'reverse))))
  2231.         (ange-ftp-put-hash-entry key dir
  2232.                      ange-ftp-expand-dir-hashtable))))
  2233.  
  2234.     ;; In the special case of CMS make sure that know the
  2235.     ;; expansion of the home minidisk now, because we will
  2236.     ;; be doing a lot of cd's.
  2237.     (if (and (eq host-type 'cms)
  2238.          (not (ange-ftp-hash-entry-exists-p
  2239.            key ange-ftp-expand-dir-hashtable)))
  2240.     (let ((dir (car (ange-ftp-get-pwd host user))))
  2241.       (if dir
  2242.           (ange-ftp-put-hash-entry key (concat "/" dir)
  2243.                        ange-ftp-expand-dir-hashtable)
  2244.         (message "Warning! Unable to get home directory")
  2245.         (sit-for 1))))))
  2246.  
  2247.  
  2248. ;;;; ------------------------------------------------------------
  2249. ;;;; Remote file and directory listing support.
  2250. ;;;; ------------------------------------------------------------
  2251.  
  2252. ;; Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands
  2253. ;; to take switch arguments.
  2254. (defun ange-ftp-dumb-unix-host (host)
  2255.   (and host ange-ftp-dumb-unix-host-regexp
  2256.        (save-match-data
  2257.      (string-match ange-ftp-dumb-unix-host-regexp host))))
  2258.  
  2259. (defun ange-ftp-add-dumb-unix-host (host)
  2260.   "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp."
  2261.   (interactive
  2262.    (list (read-string "Host: "
  2263.               (let ((name (or (buffer-file-name) default-directory)))
  2264.             (and name (car (ange-ftp-ftp-name name)))))))
  2265.   (if (not (ange-ftp-dumb-unix-host host))
  2266.       (setq ange-ftp-dumb-unix-host-regexp
  2267.         (concat "^" (regexp-quote host) "$"
  2268.             (and ange-ftp-dumb-unix-host-regexp "\\|")
  2269.             ange-ftp-dumb-unix-host-regexp)
  2270.         ange-ftp-host-cache nil)))
  2271.  
  2272. (defvar ange-ftp-parse-list-func-alist nil
  2273.   "Alist saying how to parse directory listings for certain OS types.
  2274. Association list of \( TYPE \. FUNC \) pairs.  The FUNC is a routine
  2275. which can parse the output from a DIR listing for a host of type TYPE.")
  2276.  
  2277. ;; With no-error nil, this function returns:
  2278. ;; an error if file is not an ange-ftp-name
  2279. ;;                      (This should never happen.) 
  2280. ;; an error if either the listing is unreadable or there is an ftp error.
  2281. ;; the listing (a string), if everything works.
  2282. ;; 
  2283. ;; With no-error t, it returns:
  2284. ;; an error if not an ange-ftp-name
  2285. ;; error if listing is unreadable (most likely caused by a slow connection)
  2286. ;; nil if ftp error (this is because although asking to list a nonexistent
  2287. ;;                   directory on a remote unix machine usually (except
  2288. ;;                   maybe for dumb hosts) returns an ls error, but no
  2289. ;;                   ftp error, if the same is done on a VMS machine,
  2290. ;;                   an ftp error is returned. Need to trap the error
  2291. ;;                   so we can go on and try to list the parent.)
  2292. ;; the listing, if everything works.
  2293.  
  2294. ;; If WILDCARD is non-nil, then this implements the guts of insert-directory
  2295. ;; in the wildcard case.  Then we make a relative directory listing
  2296. ;; of FILE within the directory specified by `default-directory'.
  2297.  
  2298. (defun ange-ftp-ls (file lsargs parse &optional no-error wildcard)
  2299.   "Return the output of an `DIR' or `ls' command done over ftp.
  2300. FILE is the full name of the remote file, LSARGS is any args to pass to the
  2301. `ls' command, and PARSE specifies that the output should be parsed and stored
  2302. away in the internal cache."
  2303.   ;; If parse is t, we assume that file is a directory. i.e. we only parse
  2304.   ;; full directory listings.
  2305.   (let* ((ange-ftp-this-file (ange-ftp-expand-file-name file))
  2306.      (parsed (ange-ftp-ftp-name ange-ftp-this-file)))
  2307.     (if parsed
  2308.     (let* ((host (nth 0 parsed))
  2309.            (user (nth 1 parsed))
  2310.            (name (ange-ftp-quote-string (nth 2 parsed)))
  2311.            (key (directory-file-name ange-ftp-this-file))
  2312.            (host-type (ange-ftp-host-type host user))
  2313.            (dumb (memq host-type ange-ftp-dumb-host-types))
  2314.            result
  2315.            temp
  2316.            lscmd parse-func)
  2317.       (if (string-equal name "")
  2318.           (setq name
  2319.             (ange-ftp-real-file-name-as-directory
  2320.                   (ange-ftp-expand-dir host user "~"))))
  2321.       (if (and ange-ftp-ls-cache-file
  2322.            (string-equal key ange-ftp-ls-cache-file)
  2323.            ;; Don't care about lsargs for dumb hosts.
  2324.            (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs)))
  2325.           ange-ftp-ls-cache-res
  2326.         (setq temp (ange-ftp-make-tmp-name host))
  2327.         (if wildcard
  2328.         (progn
  2329.           (ange-ftp-cd host user (file-name-directory name))
  2330.           (setq lscmd (list 'dir file temp lsargs)))
  2331.           (setq lscmd (list 'dir name temp lsargs)))
  2332.         (unwind-protect
  2333.         (if (car (setq result (ange-ftp-send-cmd
  2334.                        host
  2335.                        user
  2336.                        lscmd
  2337.                        (format "Listing %s"
  2338.                            (ange-ftp-abbreviate-filename
  2339.                         ange-ftp-this-file)))))
  2340.             (save-excursion
  2341.               (set-buffer (get-buffer-create
  2342.                    ange-ftp-data-buffer-name))
  2343.               (erase-buffer)
  2344.               (if (ange-ftp-real-file-readable-p temp)
  2345.               (ange-ftp-real-insert-file-contents temp)
  2346.             (sleep-for ange-ftp-retry-time)
  2347.                     ;wait for file to possibly appear
  2348.             (if (ange-ftp-real-file-readable-p temp)
  2349.                 ;; Try again.
  2350.                 (ange-ftp-real-insert-file-contents temp)
  2351.               (ange-ftp-error host user
  2352.                       (format
  2353.                        "list data file %s not readable"
  2354.                        temp))))
  2355.               (if parse
  2356.               (ange-ftp-set-files
  2357.                ange-ftp-this-file
  2358.                (if (setq
  2359.                 parse-func
  2360.                 (cdr (assq host-type
  2361.                        ange-ftp-parse-list-func-alist)))
  2362.                    (funcall parse-func)
  2363.                  (ange-ftp-parse-dired-listing lsargs))))
  2364.               (setq ange-ftp-ls-cache-file key
  2365.                 ange-ftp-ls-cache-lsargs lsargs
  2366.                     ; For dumb hosts-types this is
  2367.                     ; meaningless but harmless.
  2368.                 ange-ftp-ls-cache-res (buffer-string))
  2369.               ;; (kill-buffer (current-buffer))
  2370.               ange-ftp-ls-cache-res)
  2371.           (if no-error
  2372.               nil
  2373.             (ange-ftp-error host user
  2374.                     (concat "DIR failed: " (cdr result)))))
  2375.           (ange-ftp-del-tmp-name temp))))
  2376.       (error "Should never happen. Please report. Bug ref. no.: 1"))))
  2377.  
  2378. ;;;; ------------------------------------------------------------
  2379. ;;;; Directory information caching support.
  2380. ;;;; ------------------------------------------------------------
  2381.  
  2382. (defconst ange-ftp-date-regexp
  2383.   (concat
  2384.    " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct"
  2385.    "\\|Nov\\|Dec\\) +[0-3]?[0-9] "))
  2386.  
  2387. (defvar ange-ftp-add-file-entry-alist nil
  2388.   "Alist saying how to add file entries on certain OS types.
  2389. Association list of pairs \( TYPE \. FUNC \), where FUNC
  2390. is a function to be used to add a file entry for the OS TYPE. The
  2391. main reason for this alist is to deal with file versions in VMS.")
  2392.  
  2393. (defvar ange-ftp-delete-file-entry-alist nil
  2394.   "Alist saying how to delete files on certain OS types.
  2395. Association list of pairs \( TYPE \. FUNC \), where FUNC
  2396. is a function to be used to delete a file entry for the OS TYPE.
  2397. The main reason for this alist is to deal with file versions in VMS.")
  2398.  
  2399. (defun ange-ftp-add-file-entry (name &optional dir-p)
  2400.   "Add a file entry for file NAME, if its directory info exists."
  2401.   (funcall (or (cdr (assq (ange-ftp-host-type
  2402.                (car (ange-ftp-ftp-name name)))
  2403.               ange-ftp-add-file-entry-alist))
  2404.            'ange-ftp-internal-add-file-entry)
  2405.        name dir-p)
  2406.   (setq ange-ftp-ls-cache-file nil))
  2407.  
  2408. (defun ange-ftp-delete-file-entry (name &optional dir-p)
  2409.   "Delete the file entry for file NAME, if its directory info exists."
  2410.   (funcall (or (cdr (assq (ange-ftp-host-type
  2411.                (car (ange-ftp-ftp-name name)))
  2412.               ange-ftp-delete-file-entry-alist))
  2413.            'ange-ftp-internal-delete-file-entry)
  2414.        name dir-p)
  2415.   (setq ange-ftp-ls-cache-file nil))
  2416.  
  2417. (defmacro ange-ftp-parse-filename ()
  2418.   ;;Extract the filename from the current line of a dired-like listing.
  2419.   (` (let ((eol (progn (end-of-line) (point))))
  2420.        (beginning-of-line)
  2421.        (if (re-search-forward ange-ftp-date-regexp eol t)
  2422.        (progn
  2423.          (skip-chars-forward " ")
  2424.          (skip-chars-forward "^ " eol)
  2425.          (skip-chars-forward " " eol)
  2426.          ;; We bomb on filenames starting with a space.
  2427.          (buffer-substring (point) eol))))))
  2428.   
  2429. ;; This deals with the F switch. Should also do something about
  2430. ;; unquoting names obtained with the SysV b switch and the GNU Q
  2431. ;; switch. See Sebastian's dired-get-filename.
  2432.  
  2433. (defmacro ange-ftp-ls-parser ()
  2434.   ;; Note that switches is dynamically bound.
  2435.   ;; Meant to be called by ange-ftp-parse-dired-listing
  2436.   (` (let ((tbl (ange-ftp-make-hashtable))
  2437.        (used-F (and (stringp switches)
  2438.             (string-match "F" switches)))
  2439.        file-type symlink directory file)
  2440.        (while (setq file (ange-ftp-parse-filename))
  2441.      (beginning-of-line)
  2442.      (skip-chars-forward "\t 0-9")
  2443.      (setq file-type (following-char)
  2444.            directory (eq file-type ?d))
  2445.      (if (eq file-type ?l)
  2446.          (if (string-match " -> " file)
  2447.          (setq symlink (substring file (match-end 0))
  2448.                file (substring file 0 (match-beginning 0)))
  2449.            ;; Shouldn't happen
  2450.            (setq symlink ""))
  2451.        (setq symlink nil))
  2452.      ;; Only do a costly regexp search if the F switch was used.
  2453.      (if (and used-F
  2454.           (not (string-equal file ""))
  2455.           (looking-at
  2456.            ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"))
  2457.          (let ((socket (eq file-type ?s))
  2458.            (executable
  2459.             (and (not symlink) ; x bits don't mean a thing for symlinks
  2460.              (string-match "[xst]"
  2461.                        (concat
  2462.                     (buffer-substring
  2463.                      (match-beginning 1)
  2464.                      (match-end 1))
  2465.                     (buffer-substring
  2466.                      (match-beginning 2)
  2467.                      (match-end 2))
  2468.                     (buffer-substring
  2469.                      (match-beginning 3)
  2470.                      (match-end 3)))))))
  2471.            ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX)
  2472.            ;; and others don't. (sigh...) Beware, that some Unix's don't
  2473.            ;; seem to believe in the F-switch
  2474.            (if (or (and symlink (string-match "@$" file))
  2475.                (and directory (string-match "/$" file))
  2476.                (and executable (string-match "*$" file))
  2477.                (and socket (string-match "=$" file)))
  2478.            (setq file (substring file 0 -1)))))
  2479.      (ange-ftp-put-hash-entry file (or symlink directory) tbl)
  2480.      (forward-line 1))
  2481.        (ange-ftp-put-hash-entry "." t tbl)
  2482.        (ange-ftp-put-hash-entry ".." t tbl)
  2483.        tbl)))
  2484.  
  2485. ;;; The dl stuff for descriptive listings
  2486.  
  2487. (defvar ange-ftp-dl-dir-regexp nil
  2488.   "Regexp matching directories which are listed in dl format.
  2489. This regexp should not be anchored with a trailing `$', because it should
  2490. match subdirectories as well.")
  2491.  
  2492. (defun ange-ftp-add-dl-dir (dir)
  2493.   "Interactively adds a DIR to ange-ftp-dl-dir-regexp."
  2494.   (interactive
  2495.    (list (read-string "Directory: "
  2496.               (let ((name (or (buffer-file-name) default-directory)))
  2497.             (and name (ange-ftp-ftp-name name)
  2498.                  (file-name-directory name))))))
  2499.   (if (not (and ange-ftp-dl-dir-regexp
  2500.         (string-match ange-ftp-dl-dir-regexp dir)))
  2501.       (setq ange-ftp-dl-dir-regexp
  2502.         (concat "^" (regexp-quote dir)
  2503.             (and ange-ftp-dl-dir-regexp "\\|")
  2504.             ange-ftp-dl-dir-regexp))))
  2505.  
  2506. (defmacro ange-ftp-dl-parser ()
  2507.   ;; Parse the current buffer, which is assumed to be a descriptive
  2508.   ;; listing, and return a hashtable.
  2509.   (` (let ((tbl (ange-ftp-make-hashtable)))
  2510.        (while (not (eobp))
  2511.      (ange-ftp-put-hash-entry
  2512.       (buffer-substring (point)
  2513.                 (progn
  2514.                   (skip-chars-forward "^ /\n")
  2515.                   (point)))
  2516.       (eq (following-char) ?/)
  2517.       tbl)
  2518.      (forward-line 1))
  2519.        (ange-ftp-put-hash-entry "." t tbl)
  2520.        (ange-ftp-put-hash-entry ".." t tbl)
  2521.        tbl)))
  2522.  
  2523. ;; Parse the current buffer which is assumed to be in a dired-like listing
  2524. ;; format, and return a hashtable as the result. If the listing is not really
  2525. ;; a listing, then return nil.
  2526.  
  2527. (defun ange-ftp-parse-dired-listing (&optional switches)
  2528.   (save-match-data
  2529.     (cond
  2530.      ((looking-at "^total [0-9]+$")
  2531.       (forward-line 1)
  2532.       ;; Some systems put in a blank line here.
  2533.       (if (eolp) (forward-line 1))
  2534.       (ange-ftp-ls-parser))
  2535.      ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
  2536.       ;; It's an ls error message.
  2537.       nil)
  2538.      ((eobp) ; i.e. (zerop (buffer-size))
  2539.       ;; This could be one of:
  2540.       ;; (1) An Ultrix ls error message
  2541.       ;; (2) A listing with the A switch of an empty directory
  2542.       ;;     on a machine which doesn't give a total line.
  2543.       ;; (3) The twilight zone.
  2544.       ;; We'll assume (1) for now.
  2545.       nil)
  2546.      ((re-search-forward ange-ftp-date-regexp nil t)
  2547.       (beginning-of-line)
  2548.       (ange-ftp-ls-parser))
  2549.      ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t)
  2550.       ;; It's a dl listing (I hope).
  2551.       ;; file is bound by the call to ange-ftp-ls
  2552.       (ange-ftp-add-dl-dir ange-ftp-this-file)
  2553.       (beginning-of-line)
  2554.       (ange-ftp-dl-parser))
  2555.      (t nil))))
  2556.  
  2557. (defun ange-ftp-set-files (directory files)
  2558.   "For a given DIRECTORY, set or change the associated FILES hashtable."
  2559.   (and files (ange-ftp-put-hash-entry (file-name-as-directory directory)
  2560.                       files ange-ftp-files-hashtable)))
  2561.  
  2562. (defun ange-ftp-get-files (directory &optional no-error)
  2563.   "Given a given DIRECTORY, return a hashtable of file entries.
  2564. This will give an error or return nil, depending on the value of
  2565. NO-ERROR, if a listing for DIRECTORY cannot be obtained."
  2566.   (setq directory (file-name-as-directory directory)) ;normalize
  2567.   (or (ange-ftp-get-hash-entry directory ange-ftp-files-hashtable)
  2568.       (save-match-data
  2569.     (and (ange-ftp-ls directory
  2570.               ;; This is an efficiency hack. We try to
  2571.               ;; anticipate what sort of listing dired
  2572.               ;; might want, and cache just such a listing.
  2573.               (if (and (boundp 'dired-actual-switches)
  2574.                    (stringp dired-actual-switches)
  2575.                    ;; We allow the A switch, which lists
  2576.                    ;; all files except "." and "..".
  2577.                    ;; This is OK because we manually
  2578.                    ;; insert these entries
  2579.                    ;; in the hash table.
  2580.                    (string-match
  2581.                     "[aA]" dired-actual-switches)
  2582.                    (string-match
  2583.                     "l" dired-actual-switches)
  2584.                    (not (string-match
  2585.                      "R" dired-actual-switches)))
  2586.                   dired-actual-switches
  2587.                 (if (and (boundp 'dired-listing-switches)
  2588.                      (stringp dired-listing-switches)
  2589.                      (string-match
  2590.                       "[aA]" dired-listing-switches)
  2591.                      (string-match
  2592.                       "l" dired-listing-switches)
  2593.                      (not (string-match
  2594.                        "R" dired-listing-switches)))
  2595.                 dired-listing-switches
  2596.                   "-al"))
  2597.               t no-error)
  2598.          (ange-ftp-get-hash-entry
  2599.           directory ange-ftp-files-hashtable)))))
  2600.  
  2601. ;; Given NAME, return the file part that can be used for looking up the
  2602. ;; file's entry in a hashtable.
  2603. (defmacro ange-ftp-get-file-part (name)
  2604.   (` (let ((file (file-name-nondirectory (, name))))
  2605.        (if (string-equal file "")
  2606.        "."
  2607.      file))))
  2608.  
  2609. ;; Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are
  2610. ;; allowed to determine if NAME is a sub-directory by listing it directly,
  2611. ;; rather than listing its parent directory. This is used for efficiency so
  2612. ;; that a wasted listing is not done:
  2613. ;; 1. When looking for a .dired file in dired-x.el.
  2614. ;; 2. The syntax of FILE and DIR make it impossible that FILE could be a valid
  2615. ;;     subdirectory. This is of course an OS dependent judgement.
  2616.  
  2617. (defmacro ange-ftp-allow-child-lookup (dir file)
  2618.   (` (not
  2619.       (let* ((efile (, file)) ; expand once.
  2620.          (edir (, dir))
  2621.          (parsed (ange-ftp-ftp-name edir))
  2622.          (host-type (ange-ftp-host-type
  2623.              (car parsed))))
  2624.     (or
  2625. ;;; This variable seems not to exist in Emacs 19 -- rms.
  2626. ;;;     ;; Deal with dired
  2627. ;;;     (and (boundp 'dired-local-variables-file)
  2628. ;;;          (stringp dired-local-variables-file)
  2629. ;;;          (string-equal dired-local-variables-file efile))
  2630.      ;; No dots in dir names in vms.
  2631.      (and (eq host-type 'vms)
  2632.           (string-match "\\." efile))
  2633.      ;; No subdirs in mts of cms.
  2634.      (and (memq host-type '(mts cms))
  2635.           (not (string-equal "/" (nth 2 parsed)))))))))
  2636.  
  2637. (defun ange-ftp-file-entry-p (name)
  2638.   "Given NAME, return whether there is a file entry for it."
  2639.   (let* ((name (directory-file-name name))
  2640.      (dir (file-name-directory name))
  2641.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2642.      (file (ange-ftp-get-file-part name)))
  2643.     (if ent
  2644.     (ange-ftp-hash-entry-exists-p file ent)
  2645.       (or (and (ange-ftp-allow-child-lookup dir file)
  2646.            (setq ent (ange-ftp-get-files name t))
  2647.            ;; Try a child lookup. i.e. try to list file as a
  2648.            ;; subdirectory of dir. This is a good idea because
  2649.            ;; we may not have read permission for file's parent. Also,
  2650.            ;; people tend to work down directory trees anyway. We use
  2651.            ;; no-error ;; because if file does not exist as a subdir.,
  2652.            ;; then dumb hosts will give an ftp error. Smart unix hosts
  2653.            ;; will simply send back the ls
  2654.            ;; error message.
  2655.            (ange-ftp-get-hash-entry "." ent))
  2656.       ;; Child lookup failed. Try the parent. If this bombs,
  2657.       ;; we are at wits end -- signal an error.
  2658.       ;; Problem: If this signals an error, the error message
  2659.       ;; may  not have a lot to do with what went wrong.
  2660.       (ange-ftp-hash-entry-exists-p file
  2661.                     (ange-ftp-get-files dir))))))
  2662.  
  2663. (defun ange-ftp-get-file-entry (name)
  2664.   "Given NAME, return the given file entry.
  2665. The entry will be either t for a directory, nil for a normal file,
  2666. or a string for a symlink. If the file isn't in the hashtable,
  2667. this also returns nil."
  2668.   (let* ((name (directory-file-name name))
  2669.      (dir (file-name-directory name))
  2670.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2671.      (file (ange-ftp-get-file-part name)))
  2672.     (if ent
  2673.     (ange-ftp-get-hash-entry file ent)
  2674.       (or (and (ange-ftp-allow-child-lookup dir file)
  2675.            (setq ent (ange-ftp-get-files name t))
  2676.            (ange-ftp-get-hash-entry "." ent))
  2677.            ;; i.e. it's a directory by child lookup
  2678.       (ange-ftp-get-hash-entry file
  2679.                    (ange-ftp-get-files dir))))))
  2680.  
  2681. (defun ange-ftp-internal-delete-file-entry (name &optional dir-p)
  2682.   (if dir-p
  2683.       (progn 
  2684.     (setq name (file-name-as-directory name))
  2685.     (ange-ftp-del-hash-entry name ange-ftp-files-hashtable)
  2686.     (setq name (directory-file-name name))))
  2687.   ;; Note that file-name-as-directory followed by directory-file-name
  2688.   ;; serves to canonicalize directory file names to their unix form.
  2689.   ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
  2690.   (let ((files (ange-ftp-get-hash-entry (file-name-directory name)
  2691.                     ange-ftp-files-hashtable)))
  2692.     (if files
  2693.     (ange-ftp-del-hash-entry (ange-ftp-get-file-part name)
  2694.                  files))))
  2695.  
  2696. (defun ange-ftp-internal-add-file-entry (name &optional dir-p)
  2697.   (and dir-p
  2698.        (setq name (directory-file-name name)))
  2699.   (let ((files (ange-ftp-get-hash-entry (file-name-directory name)
  2700.                     ange-ftp-files-hashtable)))
  2701.     (if files
  2702.     (ange-ftp-put-hash-entry (ange-ftp-get-file-part name)
  2703.                  dir-p
  2704.                  files))))
  2705.  
  2706. (defun ange-ftp-wipe-file-entries (host user)
  2707.   "Get rid of entry for HOST, USER pair from file entry information hashtable."
  2708.   (let ((new-tbl (ange-ftp-make-hashtable (length ange-ftp-files-hashtable))))
  2709.     (ange-ftp-map-hashtable
  2710.      (function
  2711.       (lambda (key val)
  2712.     (let ((parsed (ange-ftp-ftp-name key)))
  2713.       (if parsed
  2714.           (let ((h (nth 0 parsed))
  2715.             (u (nth 1 parsed)))
  2716.         (or (and (equal host h) (equal user u))
  2717.             (ange-ftp-put-hash-entry key val new-tbl)))))))
  2718.      ange-ftp-files-hashtable)
  2719.     (setq ange-ftp-files-hashtable new-tbl)))
  2720.  
  2721. ;;;; ------------------------------------------------------------
  2722. ;;;; File transfer mode support.
  2723. ;;;; ------------------------------------------------------------
  2724.  
  2725. (defun ange-ftp-set-binary-mode (host user)
  2726.   "Tell the ftp process for the given HOST & USER to switch to binary mode."
  2727.   (let ((result (ange-ftp-send-cmd host user '(type "binary"))))
  2728.     (if (not (car result))
  2729.     (ange-ftp-error host user (concat "BINARY failed: " (cdr result)))
  2730.       (save-excursion
  2731.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2732.     (and ange-ftp-binary-hash-mark-size
  2733.          (setq ange-ftp-hash-mark-unit
  2734.            (ash ange-ftp-binary-hash-mark-size -4)))))))
  2735.  
  2736. (defun ange-ftp-set-ascii-mode (host user)
  2737.   "Tell the ftp process for the given HOST & USER to switch to ascii mode."
  2738.   (let ((result (ange-ftp-send-cmd host user '(type "ascii"))))
  2739.     (if (not (car result))
  2740.     (ange-ftp-error host user (concat "ASCII failed: " (cdr result)))
  2741.       (save-excursion
  2742.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2743.     (and ange-ftp-ascii-hash-mark-size
  2744.          (setq ange-ftp-hash-mark-unit
  2745.            (ash ange-ftp-ascii-hash-mark-size -4)))))))
  2746.  
  2747. (defun ange-ftp-cd (host user dir)
  2748.   (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD")))
  2749.     (or (car result)
  2750.     (ange-ftp-error host user (concat "CD failed: " (cdr result))))))
  2751.  
  2752. (defun ange-ftp-get-pwd (host user)
  2753.   "Attempts to get the current working directory for the given HOST/USER pair.
  2754. Returns \( DIR . LINE \) where DIR is either the directory or NIL if not found,
  2755. and LINE is the relevant success or fail line from the FTP-client."
  2756.   (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD"))
  2757.      (line (cdr result))
  2758.      dir)
  2759.     (if (car result)
  2760.     (save-match-data
  2761.       (and (or (string-match "\"\\([^\"]*\\)\"" line)
  2762.            (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers!
  2763.            (setq dir (substring line
  2764.                     (match-beginning 1)
  2765.                     (match-end 1))))))
  2766.     (cons dir line)))
  2767.  
  2768. ;;; ------------------------------------------------------------
  2769. ;;; expand-file-name and friends...which currently don't work
  2770. ;;; ------------------------------------------------------------
  2771.  
  2772. (defun ange-ftp-expand-dir (host user dir)
  2773.   "Return the result of doing a PWD in the current FTP session.
  2774. Use the connection to machine HOST
  2775. logged in as user USER and cd'd to directory DIR."
  2776.   (let* ((host-type (ange-ftp-host-type host user))
  2777.      ;; It is more efficient to call ange-ftp-host-type
  2778.      ;; before binding res, because ange-ftp-host-type sometimes
  2779.      ;; adds to the info in the expand-dir-hashtable.
  2780.      (fix-name-func
  2781.       (cdr (assq host-type ange-ftp-fix-name-func-alist)))
  2782.      (key (concat host "/" user "/" dir))
  2783.      (res (ange-ftp-get-hash-entry key ange-ftp-expand-dir-hashtable)))
  2784.     (or res
  2785.     (progn
  2786.       (or
  2787.        (string-equal user "anonymous")
  2788.        (string-equal user "ftp")
  2789.        (not (eq host-type 'unix))
  2790.        (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp
  2791.                           "\\|"
  2792.                           ange-ftp-good-msgs))
  2793.           (result (ange-ftp-send-cmd host user
  2794.                          (list 'get dir "/dev/null")
  2795.                          (format "expanding %s" dir)))
  2796.           (line (cdr result)))
  2797.          (setq res
  2798.            (if (string-match ange-ftp-expand-dir-regexp line)
  2799.                (substring line 
  2800.                   (match-beginning 1)
  2801.                   (match-end 1))))))
  2802.       (or res
  2803.           (if (string-equal dir "~")
  2804.           (setq res (car (ange-ftp-get-pwd host user)))
  2805.         (let ((home (ange-ftp-expand-dir host user "~")))
  2806.           (unwind-protect
  2807.               (and (ange-ftp-cd host user dir)
  2808.                (setq res (car (ange-ftp-get-pwd host user))))
  2809.             (ange-ftp-cd host user home)))))
  2810.       (if res
  2811.           (let ((ange-ftp-this-user user)
  2812.             (ange-ftp-this-host host))
  2813.         (if fix-name-func
  2814.             (setq res (funcall fix-name-func res 'reverse)))
  2815.         (ange-ftp-put-hash-entry
  2816.          key res ange-ftp-expand-dir-hashtable)))
  2817.       res))))
  2818.  
  2819. (defun ange-ftp-canonize-filename (n)
  2820.   "Take a string and short-circuit //, /. and /.."
  2821.   (if (string-match "[^:]+//" n)        ;don't upset Apollo users
  2822.       (setq n (substring n (1- (match-end 0)))))
  2823.   (let ((parsed (ange-ftp-ftp-name n)))
  2824.     (if parsed
  2825.     (let ((host (car parsed))
  2826.           (user (nth 1 parsed))
  2827.           (name (nth 2 parsed)))
  2828.       
  2829.       ;; See if remote name is absolute.  If so then just expand it and
  2830.       ;; replace the name component of the overall name.
  2831.       (cond ((string-match "^/" name)
  2832.          name)
  2833.         
  2834.         ;; Name starts with ~ or ~user.  Resolve that part of the name
  2835.         ;; making it absolute then re-expand it.
  2836.         ((string-match "^~[^/]*" name)
  2837.          (let* ((tilda (substring name
  2838.                       (match-beginning 0) 
  2839.                       (match-end 0)))
  2840.             (rest (substring name (match-end 0)))
  2841.             (dir (ange-ftp-expand-dir host user tilda)))
  2842.            (if dir
  2843.                (setq name (concat dir rest))
  2844.              (error "User \"%s\" is not known" 
  2845.                 (substring tilda 1)))))
  2846.         
  2847.         ;; relative name.  Tack on homedir and re-expand.
  2848.         (t
  2849.          (let ((dir (ange-ftp-expand-dir host user "~")))
  2850.            (if dir
  2851.                (setq name (concat
  2852.                    (ange-ftp-real-file-name-as-directory dir)
  2853.                    name))
  2854.              (error "Unable to obtain CWD")))))
  2855.       
  2856.       ;; If name starts with //, preserve that, for apollo system.
  2857.       (if (not (string-match "^//" name))
  2858.           (progn
  2859.         (setq name (ange-ftp-remote-expand-file-name name))
  2860.  
  2861.         (if (string-match "^//" name)
  2862.             (setq name (substring name 1)))))
  2863.       
  2864.       ;; Now substitute the expanded name back into the overall filename.
  2865.       (ange-ftp-replace-name-component n name))
  2866.       
  2867.       ;; non-ange-ftp name.  Just expand normally.
  2868.       (if (file-name-absolute-p n)
  2869.       (ange-ftp-real-expand-file-name n)
  2870.     (ange-ftp-remote-expand-file-name
  2871.      (ange-ftp-real-file-name-nondirectory n)
  2872.      (ange-ftp-real-file-name-directory n))))))
  2873.  
  2874. (defun ange-ftp-expand-file-name (name &optional default)
  2875.   "Documented as original."
  2876.   (save-match-data
  2877.     (if (eq (string-to-char name) ?/)
  2878.     (while (cond ((string-match "[^:]+//" name) ;don't upset Apollo users
  2879.               (setq name (substring name (1- (match-end 0)))))
  2880.              ((string-match "/~" name)
  2881.               (setq name (substring name (1- (match-end 0))))))))
  2882.     (cond ((eq (string-to-char name) ?~)
  2883.        (ange-ftp-real-expand-file-name name))
  2884.       ((file-name-absolute-p name)
  2885.        (ange-ftp-canonize-filename name))
  2886.       ((zerop (length name))
  2887.        (ange-ftp-canonize-filename (or default default-directory)))
  2888.       ((ange-ftp-canonize-filename
  2889.         (concat (file-name-as-directory (or default default-directory))
  2890.             name))))))
  2891.  
  2892. ;;; These are problems--they are currently not enabled.
  2893.  
  2894. (defvar ange-ftp-file-name-as-directory-alist nil
  2895.   "Association list of \( TYPE \. FUNC \) pairs.
  2896. FUNC converts a filename to a directory name for the operating
  2897. system TYPE.")
  2898.  
  2899. (defun ange-ftp-file-name-as-directory (name)
  2900.   "Documented as original."
  2901.   (let ((parsed (ange-ftp-ftp-name name)))
  2902.     (if parsed
  2903.     (if (string-equal (nth 2 parsed) "")
  2904.         name
  2905.       (funcall (or (cdr (assq
  2906.                  (ange-ftp-host-type (car parsed))
  2907.                  ange-ftp-file-name-as-directory-alist))
  2908.                'ange-ftp-real-file-name-as-directory)
  2909.            name))
  2910.       (ange-ftp-real-file-name-as-directory name))))
  2911.      
  2912. (defun ange-ftp-file-name-directory (name)
  2913.   "Documented as original."
  2914.   (let ((parsed (ange-ftp-ftp-name name)))
  2915.     (if parsed
  2916.     (let ((filename (nth 2 parsed)))
  2917.       (if (save-match-data
  2918.         (string-match "^~[^/]*$" filename))
  2919.           name
  2920.         (ange-ftp-replace-name-component
  2921.          name
  2922.          (ange-ftp-real-file-name-directory filename))))
  2923.       (ange-ftp-real-file-name-directory name))))
  2924.  
  2925. (defun ange-ftp-file-name-nondirectory (name)
  2926.   "Documented as original."
  2927.   (let ((parsed (ange-ftp-ftp-name name)))
  2928.     (if parsed
  2929.     (let ((filename (nth 2 parsed)))
  2930.       (if (save-match-data
  2931.         (string-match "^~[^/]*$" filename))
  2932.           ""
  2933.         (ange-ftp-real-file-name-nondirectory name)))
  2934.       (ange-ftp-real-file-name-nondirectory name))))
  2935.  
  2936. (defun ange-ftp-directory-file-name (dir)
  2937.   "Documented as original."
  2938.   (let ((parsed (ange-ftp-ftp-name dir)))
  2939.     (if parsed
  2940.     (ange-ftp-replace-name-component
  2941.        dir
  2942.        (ange-ftp-real-directory-file-name (nth 2 parsed)))
  2943.       (ange-ftp-real-directory-file-name dir))))
  2944.  
  2945.  
  2946. ;;; Hooks that handle Emacs primitives.
  2947.  
  2948. ;; Returns non-nil if should transfer FILE in binary mode.
  2949. (defun ange-ftp-binary-file (file)
  2950.   (save-match-data
  2951.     (string-match ange-ftp-binary-file-name-regexp file)))
  2952.  
  2953. (defun ange-ftp-write-region (start end filename &optional append visit)
  2954.   (setq filename (expand-file-name filename))
  2955.   (let ((parsed (ange-ftp-ftp-name filename)))
  2956.     (if parsed
  2957.     (let* ((host (nth 0 parsed))
  2958.            (user (nth 1 parsed))
  2959.            (name (ange-ftp-quote-string (nth 2 parsed)))
  2960.            (temp (ange-ftp-make-tmp-name host))
  2961.            (binary (or (ange-ftp-binary-file filename)
  2962.                (eq (ange-ftp-host-type host user) 'unix)))
  2963.            (cmd (if append 'append 'put))
  2964.            (abbr (ange-ftp-abbreviate-filename filename)))
  2965.       (unwind-protect
  2966.           (progn
  2967.         (let ((executing-kbd-macro t)
  2968.               (filename (buffer-file-name))
  2969.               (mod-p (buffer-modified-p)))
  2970.           (unwind-protect
  2971.               (ange-ftp-real-write-region start end temp nil visit)
  2972.             ;; cleanup forms
  2973.             (setq buffer-file-name filename)
  2974.             (set-buffer-modified-p mod-p)))
  2975.         (if binary
  2976.             (ange-ftp-set-binary-mode host user))
  2977.  
  2978.         ;; tell the process filter what size the transfer will be.
  2979.         (let ((attr (file-attributes temp)))
  2980.           (if attr
  2981.               (ange-ftp-set-xfer-size host user (nth 7 attr))))
  2982.  
  2983.         ;; put or append the file.
  2984.         (let ((result (ange-ftp-send-cmd host user
  2985.                          (list cmd temp name)
  2986.                          (format "Writing %s" abbr))))
  2987.           (or (car result)
  2988.               (signal 'ftp-error
  2989.                   (list
  2990.                    "Opening output file"
  2991.                    (format "FTP Error: \"%s\"" (cdr result))
  2992.                    filename)))))
  2993.         (ange-ftp-del-tmp-name temp)
  2994.         (if binary 
  2995.         (ange-ftp-set-ascii-mode host user)))
  2996.       (if (eq visit t)
  2997.           (progn
  2998.         (set-visited-file-modtime '(0 0))
  2999.         (ange-ftp-set-buffer-mode)
  3000.         (setq buffer-file-name filename)
  3001.         (set-buffer-modified-p nil)))
  3002.       (ange-ftp-message "Wrote %s" abbr)
  3003.       (ange-ftp-add-file-entry filename))
  3004.       (ange-ftp-real-write-region start end filename append visit))))
  3005.  
  3006. (defun ange-ftp-insert-file-contents (filename &optional visit beg end replace)
  3007.   (barf-if-buffer-read-only)
  3008.   (setq filename (expand-file-name filename))
  3009.   (let ((parsed (ange-ftp-ftp-name filename)))
  3010.     (if parsed
  3011.     (progn
  3012.       (if visit
  3013.           (setq buffer-file-name filename))
  3014.       (if (or (file-exists-p filename)
  3015.           (progn
  3016.             (setq ange-ftp-ls-cache-file nil)
  3017.             (ange-ftp-del-hash-entry (file-name-directory filename)
  3018.                          ange-ftp-files-hashtable)
  3019.             (file-exists-p filename)))
  3020.           (let* ((host (nth 0 parsed))
  3021.              (user (nth 1 parsed))
  3022.              (name (ange-ftp-quote-string (nth 2 parsed)))
  3023.              (temp (ange-ftp-make-tmp-name host))
  3024.              (binary (or (ange-ftp-binary-file filename)
  3025.                  (eq (ange-ftp-host-type host user) 'unix)))
  3026.              (abbr (ange-ftp-abbreviate-filename filename))
  3027.              size)
  3028.         (unwind-protect
  3029.             (progn
  3030.               (if binary
  3031.               (ange-ftp-set-binary-mode host user))
  3032.               (let ((result (ange-ftp-send-cmd host user
  3033.                           (list 'get name temp)
  3034.                           (format "Retrieving %s" abbr))))
  3035.             (or (car result)
  3036.                 (signal 'ftp-error
  3037.                     (list
  3038.                      "Opening input file"
  3039.                      (format "FTP Error: \"%s\"" (cdr result))
  3040.                      filename))))
  3041.               (if (or (ange-ftp-real-file-readable-p temp)
  3042.                   (sleep-for ange-ftp-retry-time)
  3043.                   ;; Wait for file to hopefully appear.
  3044.                   (ange-ftp-real-file-readable-p temp))
  3045.               (setq
  3046.                size
  3047.                (nth 1 (ange-ftp-real-insert-file-contents
  3048.                    temp visit beg end replace)))
  3049.             (signal 'ftp-error
  3050.                 (list
  3051.                  "Opening input file:"
  3052.                  (format
  3053.                   "FTP Error: %s not arrived or readable"
  3054.                   filename)))))
  3055.           (if binary
  3056.               (ange-ftp-set-ascii-mode host user))
  3057.           (ange-ftp-del-tmp-name temp))
  3058.         (if visit
  3059.             (progn
  3060.               (set-visited-file-modtime '(0 0))
  3061.               (setq buffer-file-name filename)))
  3062.         (list filename size))
  3063.         (signal 'file-error
  3064.             (list 
  3065.              "Opening input file"
  3066.              filename))))
  3067.       (ange-ftp-real-insert-file-contents filename visit beg end replace))))
  3068.  
  3069. (defun ange-ftp-expand-symlink (file dir)
  3070.   (if (file-name-absolute-p file)
  3071.       (ange-ftp-replace-name-component dir file)
  3072.     (expand-file-name file dir)))
  3073.  
  3074. (defun ange-ftp-file-symlink-p (file)
  3075.   ;; call ange-ftp-expand-file-name rather than the normal
  3076.   ;; expand-file-name to stop loops when using a package that
  3077.   ;; redefines both file-symlink-p and expand-file-name.
  3078.   (setq file (ange-ftp-expand-file-name file))
  3079.   (if (ange-ftp-ftp-name file)
  3080.       (let ((file-ent
  3081.          (ange-ftp-get-hash-entry
  3082.           (ange-ftp-get-file-part file)
  3083.           (ange-ftp-get-files (file-name-directory file)))))
  3084.     (if (stringp file-ent)
  3085.         (if (file-name-absolute-p file-ent)
  3086.         (ange-ftp-replace-name-component
  3087.               (file-name-directory file) file-ent)
  3088.           file-ent)))
  3089.     (ange-ftp-real-file-symlink-p file)))
  3090.  
  3091. (defun ange-ftp-file-exists-p (name)
  3092.   (setq name (expand-file-name name))
  3093.   (if (ange-ftp-ftp-name name)
  3094.       (if (ange-ftp-file-entry-p name)
  3095.       (let ((file-ent (ange-ftp-get-file-entry name)))
  3096.         (if (stringp file-ent)
  3097.         (file-exists-p
  3098.          (ange-ftp-expand-symlink file-ent
  3099.                       (file-name-directory
  3100.                        (directory-file-name name))))
  3101.           t)))
  3102.     (ange-ftp-real-file-exists-p name)))
  3103.  
  3104. (defun ange-ftp-file-directory-p (name)
  3105.   (setq name (expand-file-name name))
  3106.   (if (ange-ftp-ftp-name name)
  3107.       ;; We do a file-name-as-directory on name here because some
  3108.       ;; machines (VMS) use a .DIR to indicate the filename associated
  3109.       ;; with a directory. This needs to be canonicalized.
  3110.       (let ((file-ent (ange-ftp-get-file-entry
  3111.                (ange-ftp-file-name-as-directory name))))
  3112.     (if (stringp file-ent)
  3113.         (file-directory-p
  3114.          (ange-ftp-expand-symlink file-ent
  3115.                       (file-name-directory
  3116.                        (directory-file-name name))))
  3117.       file-ent))
  3118.     (ange-ftp-real-file-directory-p name)))
  3119.  
  3120. (defun ange-ftp-directory-files (directory &optional full match
  3121.                        &rest v19-args)
  3122.   (setq directory (expand-file-name directory))
  3123.   (if (ange-ftp-ftp-name directory)
  3124.       (progn
  3125.     (ange-ftp-barf-if-not-directory directory)
  3126.     (let ((tail (ange-ftp-hash-table-keys
  3127.              (ange-ftp-get-files directory)))
  3128.           files f)
  3129.       (setq directory (file-name-as-directory directory))
  3130.       (save-match-data
  3131.         (while tail
  3132.           (setq f (car tail)
  3133.             tail (cdr tail))
  3134.           (if (or (not match) (string-match match f))
  3135.           (setq files
  3136.             (cons (if full (concat directory f) f) files)))))
  3137.       (nreverse files)))
  3138.     (apply 'ange-ftp-real-directory-files directory full match v19-args)))
  3139.  
  3140. (defun ange-ftp-file-attributes (file)
  3141.   (setq file (expand-file-name file))
  3142.   (let ((parsed (ange-ftp-ftp-name file)))
  3143.     (if parsed
  3144.     (let ((part (ange-ftp-get-file-part file))
  3145.           (files (ange-ftp-get-files (file-name-directory file))))
  3146.       (if (ange-ftp-hash-entry-exists-p part files)
  3147.           (let ((host (nth 0 parsed))
  3148.             (user (nth 1 parsed))
  3149.             (name (nth 2 parsed))
  3150.             (dirp (ange-ftp-get-hash-entry part files)))
  3151.         (list (if (and (stringp dirp) (file-name-absolute-p dirp))
  3152.               (ange-ftp-expand-symlink dirp
  3153.                            (file-name-directory file))
  3154.             dirp)        ;0 file type
  3155.               -1        ;1 link count
  3156.               -1        ;2 uid
  3157.               -1        ;3 gid
  3158.               '(0 0)        ;4 atime
  3159.               '(0 0)        ;5 mtime
  3160.               '(0 0)        ;6 ctime
  3161.               -1        ;7 size
  3162.               (concat (if (stringp dirp) "l" (if dirp "d" "-"))
  3163.                   "?????????") ;8 mode
  3164.               nil        ;9 gid weird
  3165.               ;; Hack to give remote files a unique "inode number".
  3166.               ;; It's actually the sum of the characters in its name.
  3167.               (apply '+ (nconc (mapcar 'identity host)
  3168.                        (mapcar 'identity user)
  3169.                        (mapcar 'identity
  3170.                            (directory-file-name name))))
  3171.               -1        ;11 device number [v19 only]
  3172.               ))))
  3173.       (ange-ftp-real-file-attributes file))))
  3174.  
  3175. (defun ange-ftp-file-writable-p (file)
  3176.   (setq file (expand-file-name file))
  3177.   (if (ange-ftp-ftp-name file)
  3178.       (or (file-exists-p file)        ;guess here for speed
  3179.       (file-directory-p (file-name-directory file)))
  3180.     (ange-ftp-real-file-writable-p file)))
  3181.  
  3182. (defun ange-ftp-file-readable-p (file)
  3183.   (setq file (expand-file-name file))
  3184.   (if (ange-ftp-ftp-name file)
  3185.       (file-exists-p file)
  3186.     (ange-ftp-real-file-readable-p file)))
  3187.  
  3188. (defun ange-ftp-file-executable-p (file)
  3189.   (setq file (expand-file-name file))
  3190.   (if (ange-ftp-ftp-name file)
  3191.       (file-exists-p file)
  3192.     (ange-ftp-real-file-executable-p file)))
  3193.  
  3194. (defun ange-ftp-delete-file (file)
  3195.   (interactive "fDelete file: ")
  3196.   (setq file (expand-file-name file))
  3197.   (let ((parsed (ange-ftp-ftp-name file)))
  3198.     (if parsed
  3199.     (let* ((host (nth 0 parsed))
  3200.            (user (nth 1 parsed))
  3201.            (name (ange-ftp-quote-string (nth 2 parsed)))
  3202.            (abbr (ange-ftp-abbreviate-filename file))
  3203.            (result (ange-ftp-send-cmd host user
  3204.                       (list 'delete name)
  3205.                       (format "Deleting %s" abbr))))
  3206.       (or (car result)
  3207.           (signal 'ftp-error
  3208.               (list
  3209.                "Removing old name"
  3210.                (format "FTP Error: \"%s\"" (cdr result))
  3211.                file)))
  3212.       (ange-ftp-delete-file-entry file))
  3213.       (ange-ftp-real-delete-file file))))
  3214.  
  3215. (defun ange-ftp-verify-visited-file-modtime (buf)
  3216.   (let ((name (buffer-file-name buf)))
  3217.     (if (and (stringp name) (ange-ftp-ftp-name name))
  3218.     t
  3219.       (ange-ftp-real-verify-visited-file-modtime buf))))
  3220.  
  3221. ;;;; ------------------------------------------------------------
  3222. ;;;; File copying support... totally re-written 6/24/92.
  3223. ;;;; ------------------------------------------------------------
  3224.  
  3225. (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive)
  3226.   (if (file-exists-p absname)
  3227.       (if (not interactive)
  3228.       (signal 'file-already-exists (list absname))
  3229.     (if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
  3230.                       absname querystring)))
  3231.         (signal 'file-already-exists (list absname))))))
  3232.  
  3233. ;; async local copy commented out for now since I don't seem to get
  3234. ;; the process sentinel called for some processes.
  3235. ;;
  3236. ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists
  3237. ;;                         keep-date cont)
  3238. ;;   "Kludge to copy a local file and call a continuation when the copy
  3239. ;; finishes."
  3240. ;;   ;; check to see if we can overwrite
  3241. ;;   (if (or (not ok-if-already-exists)
  3242. ;;       (numberp ok-if-already-exists))
  3243. ;;       (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3244. ;;                          (numberp ok-if-already-exists)))
  3245. ;;   (let ((proc (start-process " *copy*"
  3246. ;;                  (generate-new-buffer "*copy*")
  3247. ;;                  "cp"
  3248. ;;                  filename
  3249. ;;                  newname))
  3250. ;;     res)
  3251. ;;     (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel))
  3252. ;;     (process-kill-without-query proc)
  3253. ;;     (save-excursion
  3254. ;;       (set-buffer (process-buffer proc))
  3255. ;;       (make-variable-buffer-local 'copy-cont)
  3256. ;;       (setq copy-cont cont))))
  3257. ;; 
  3258. ;; (defun ange-ftp-copy-file-locally-sentinel (proc status)
  3259. ;;   (save-excursion
  3260. ;;     (set-buffer (process-buffer proc))
  3261. ;;     (let ((cont copy-cont)
  3262. ;;       (result (buffer-string)))
  3263. ;;       (unwind-protect
  3264. ;;       (if (and (string-equal status "finished\n")
  3265. ;;            (zerop (length result)))
  3266. ;;           (ange-ftp-call-cont cont t nil)
  3267. ;;         (ange-ftp-call-cont cont
  3268. ;;                 nil
  3269. ;;                 (if (zerop (length result))
  3270. ;;                     (substring status 0 -1)
  3271. ;;                   (substring result 0 -1))))
  3272. ;;     (kill-buffer (current-buffer))))))
  3273.  
  3274. ;; this is the extended version of ange-ftp-copy-file-internal that works
  3275. ;; asynchronously if asked nicely.
  3276. (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists
  3277.                          keep-date &optional msg cont nowait)
  3278.   (setq filename (expand-file-name filename)
  3279.     newname (expand-file-name newname))
  3280.  
  3281.   ;; canonicalize newname if a directory.
  3282.   (if (file-directory-p newname)
  3283.       (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
  3284.  
  3285.   (let ((f-parsed (ange-ftp-ftp-name filename))
  3286.     (t-parsed (ange-ftp-ftp-name newname)))
  3287.  
  3288.     ;; local file to local file copy?
  3289.     (if (and (not f-parsed) (not t-parsed))
  3290.     (progn
  3291.       (ange-ftp-real-copy-file filename newname ok-if-already-exists
  3292.                    keep-date)
  3293.       (if cont
  3294.           (ange-ftp-call-cont cont t "Copied locally")))
  3295.       ;; one or both files are remote.
  3296.       (let* ((f-host (and f-parsed (nth 0 f-parsed)))
  3297.          (f-user (and f-parsed (nth 1 f-parsed)))
  3298.          (f-name (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed))))
  3299.          (f-abbr (ange-ftp-abbreviate-filename filename))
  3300.          (t-host (and t-parsed (nth 0 t-parsed)))
  3301.          (t-user (and t-parsed (nth 1 t-parsed)))
  3302.          (t-name (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed))))
  3303.          (t-abbr (ange-ftp-abbreviate-filename newname filename))
  3304.          (binary (or (ange-ftp-binary-file filename)
  3305.              (ange-ftp-binary-file newname)
  3306.              (and (eq (ange-ftp-host-type f-host f-user) 'unix)
  3307.                   (eq (ange-ftp-host-type t-host t-user) 'unix))))
  3308.          temp1
  3309.          temp2)
  3310.  
  3311.     ;; check to see if we can overwrite
  3312.     (if (or (not ok-if-already-exists)
  3313.         (numberp ok-if-already-exists))
  3314.         (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3315.                            (numberp ok-if-already-exists)))
  3316.  
  3317.     ;; do the copying.
  3318.     (if f-parsed
  3319.         
  3320.         ;; filename was remote.
  3321.         (progn
  3322.           (if (or (ange-ftp-use-gateway-p f-host)
  3323.               t-parsed)
  3324.           ;; have to use intermediate file if we are getting via
  3325.           ;; gateway machine or we are doing a remote to remote copy.
  3326.           (setq temp1 (ange-ftp-make-tmp-name f-host)))
  3327.           
  3328.           (if binary
  3329.           (ange-ftp-set-binary-mode f-host f-user))
  3330.  
  3331.           (ange-ftp-send-cmd
  3332.            f-host
  3333.            f-user
  3334.            (list 'get f-name (or temp1 newname))
  3335.            (or msg
  3336.            (if (and temp1 t-parsed)
  3337.                (format "Getting %s" f-abbr)
  3338.              (format "Copying %s to %s" f-abbr t-abbr)))
  3339.            (list (function ange-ftp-cf1)
  3340.              filename newname binary msg
  3341.              f-parsed f-host f-user f-name f-abbr
  3342.              t-parsed t-host t-user t-name t-abbr
  3343.              temp1 temp2 cont nowait)
  3344.            nowait))
  3345.  
  3346.       ;; filename wasn't remote.  newname must be remote.  call the
  3347.       ;; function which does the remainder of the copying work.
  3348.       (ange-ftp-cf1 t nil
  3349.             filename newname binary msg
  3350.             f-parsed f-host f-user f-name f-abbr
  3351.             t-parsed t-host t-user t-name t-abbr
  3352.             nil nil cont nowait))))))
  3353.  
  3354. ;; next part of copying routine.
  3355. (defun ange-ftp-cf1 (result line
  3356.                 filename newname binary msg
  3357.                 f-parsed f-host f-user f-name f-abbr
  3358.                 t-parsed t-host t-user t-name t-abbr
  3359.                 temp1 temp2 cont nowait)
  3360.   (if line
  3361.       ;; filename must have been remote, and we must have just done a GET.
  3362.       (unwind-protect
  3363.       (or result
  3364.           ;; GET failed for some reason.  Clean up and get out.
  3365.           (progn
  3366.         (and temp1 (ange-ftp-del-tmp-name temp1))
  3367.         (or cont
  3368.             (signal 'ftp-error (list "Opening input file"
  3369.                          (format "FTP Error: \"%s\"" line)
  3370.                          filename)))))
  3371.     ;; cleanup
  3372.     (if binary
  3373.         (ange-ftp-set-ascii-mode f-host f-user))))
  3374.  
  3375.   (if result
  3376.       ;; We now have to copy either temp1 or filename to newname.
  3377.       (if t-parsed
  3378.       
  3379.       ;; newname was remote.
  3380.       (progn
  3381.         (if (ange-ftp-use-gateway-p t-host)
  3382.         (setq temp2 (ange-ftp-make-tmp-name t-host)))
  3383.         
  3384.         ;; make sure data is moved into the right place for the
  3385.         ;; outgoing transfer.  gateway temporary files complicate
  3386.         ;; things nicely.
  3387.         (if temp1
  3388.         (if temp2
  3389.             (if (string-equal temp1 temp2)
  3390.             (setq temp1 nil)
  3391.               (ange-ftp-real-copy-file temp1 temp2 t))
  3392.           (setq temp2 temp1 temp1 nil))
  3393.           (if temp2
  3394.           (ange-ftp-real-copy-file filename temp2 t)))
  3395.         
  3396.         (if binary
  3397.         (ange-ftp-set-binary-mode t-host t-user))
  3398.  
  3399.         ;; tell the process filter what size the file is.
  3400.         (let ((attr (file-attributes (or temp2 filename))))
  3401.           (if attr
  3402.           (ange-ftp-set-xfer-size t-host t-user (nth 7 attr))))
  3403.  
  3404.         (ange-ftp-send-cmd
  3405.          t-host
  3406.          t-user
  3407.          (list 'put (or temp2 filename) t-name)
  3408.          (or msg
  3409.          (if (and temp2 f-parsed)
  3410.              (format "Putting %s" newname)
  3411.            (format "Copying %s to %s" f-abbr t-abbr)))
  3412.          (list (function ange-ftp-cf2)
  3413.            newname t-host t-user binary temp1 temp2 cont)
  3414.          nowait))
  3415.     
  3416.     ;; newname wasn't remote.
  3417.     (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont))
  3418.  
  3419.     ;; first copy failed, tell caller
  3420.     (ange-ftp-call-cont cont result line)))
  3421.  
  3422. ;; last part of copying routine.
  3423. (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont)
  3424.   (unwind-protect
  3425.       (if line
  3426.       ;; result from doing a local to remote copy.
  3427.       (unwind-protect
  3428.           (progn
  3429.         (or result
  3430.             (or cont
  3431.             (signal 'ftp-error
  3432.                 (list "Opening output file"
  3433.                       (format "FTP Error: \"%s\"" line)
  3434.                       newname))))
  3435.         
  3436.         (ange-ftp-add-file-entry newname))
  3437.         
  3438.         ;; cleanup.
  3439.         (if binary
  3440.         (ange-ftp-set-ascii-mode t-host t-user)))
  3441.           
  3442.     ;; newname was local.
  3443.     (if temp1
  3444.         (ange-ftp-real-copy-file temp1 newname t)))
  3445.       
  3446.     ;; clean up
  3447.     (and temp1 (ange-ftp-del-tmp-name temp1))
  3448.     (and temp2 (ange-ftp-del-tmp-name temp2))
  3449.     (ange-ftp-call-cont cont result line)))
  3450.  
  3451. (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists
  3452.                     keep-date)
  3453.   (interactive "fCopy file: \nFCopy %s to file: \np")
  3454.   (ange-ftp-copy-file-internal filename
  3455.                    newname
  3456.                    ok-if-already-exists
  3457.                    keep-date
  3458.                    nil
  3459.                    nil
  3460.                    (interactive-p)))
  3461.  
  3462. ;;;; ------------------------------------------------------------
  3463. ;;;; File renaming support.
  3464. ;;;; ------------------------------------------------------------
  3465.  
  3466. (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed)
  3467.   "Rename remote file FILE to remote file NEWNAME."
  3468.   (let ((f-host (nth 0 f-parsed))
  3469.     (f-user (nth 1 f-parsed))
  3470.     (t-host (nth 0 t-parsed))
  3471.     (t-user (nth 1 t-parsed)))
  3472.     (if (and (string-equal f-host t-host)
  3473.          (string-equal f-user t-user))
  3474.     (let* ((f-name (ange-ftp-quote-string (nth 2 f-parsed)))
  3475.            (t-name (ange-ftp-quote-string (nth 2 t-parsed)))
  3476.            (cmd (list 'rename f-name t-name))
  3477.            (fabbr (ange-ftp-abbreviate-filename filename))
  3478.            (nabbr (ange-ftp-abbreviate-filename newname filename))
  3479.            (result (ange-ftp-send-cmd f-host f-user cmd
  3480.                       (format "Renaming %s to %s"
  3481.                           fabbr
  3482.                           nabbr))))
  3483.       (or (car result)
  3484.           (signal 'ftp-error
  3485.               (list
  3486.                "Renaming"
  3487.                (format "FTP Error: \"%s\"" (cdr result))
  3488.                filename
  3489.                newname)))
  3490.       (ange-ftp-add-file-entry newname)
  3491.       (ange-ftp-delete-file-entry filename))
  3492.       (ange-ftp-copy-file-internal filename newname t nil)
  3493.       (delete-file filename))))
  3494.  
  3495. (defun ange-ftp-rename-local-to-remote (filename newname)
  3496.   "Rename local FILENAME to remote file NEWNAME."
  3497.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3498.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3499.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3500.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3501.     (let (ange-ftp-process-verbose)
  3502.       (delete-file filename))))
  3503.  
  3504. (defun ange-ftp-rename-remote-to-local (filename newname)
  3505.   "Rename remote file FILENAME to local file NEWNAME."
  3506.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3507.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3508.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3509.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3510.     (let (ange-ftp-process-verbose)
  3511.       (delete-file filename))))
  3512.  
  3513. (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists)
  3514.   (interactive "fRename file: \nFRename %s to file: \np")
  3515.   (setq filename (expand-file-name filename))
  3516.   (setq newname (expand-file-name newname))
  3517.   (let* ((f-parsed (ange-ftp-ftp-name filename))
  3518.      (t-parsed (ange-ftp-ftp-name newname)))
  3519.     (if (and (or f-parsed t-parsed)
  3520.          (or (not ok-if-already-exists)
  3521.          (numberp ok-if-already-exists)))
  3522.     (ange-ftp-barf-or-query-if-file-exists
  3523.      newname
  3524.      "rename to it"
  3525.      (numberp ok-if-already-exists)))
  3526.     (if f-parsed
  3527.     (if t-parsed
  3528.         (ange-ftp-rename-remote-to-remote filename newname f-parsed
  3529.                           t-parsed)
  3530.       (ange-ftp-rename-remote-to-local filename newname))
  3531.       (if t-parsed
  3532.       (ange-ftp-rename-local-to-remote filename newname)
  3533.     (ange-ftp-real-rename-file filename newname ok-if-already-exists)))))
  3534.  
  3535. ;;;; ------------------------------------------------------------
  3536. ;;;; File name completion support.
  3537. ;;;; ------------------------------------------------------------
  3538.  
  3539. ;; If the file entry SYM is a symlink, returns whether its file exists.
  3540. ;; Note that `ange-ftp-this-dir' is used as a free variable.
  3541. (defun ange-ftp-file-entry-active-p (sym)
  3542.   (let ((val (get sym 'val)))
  3543.     (or (not (stringp val))
  3544.     (file-exists-p (ange-ftp-expand-symlink val ange-ftp-this-dir)))))
  3545.  
  3546. ;; If the file entry is not a directory (nor a symlink pointing to a directory)
  3547. ;; returns whether the file (or file pointed to by the symlink) is ignored
  3548. ;; by completion-ignored-extensions.
  3549. ;; Note that `ange-ftp-this-dir' and `ange-ftp-completion-ignored-pattern'
  3550. ;; are used as free variables.
  3551. (defun ange-ftp-file-entry-not-ignored-p (sym)
  3552.   (let ((val (get sym 'val))
  3553.     (symname (symbol-name sym)))
  3554.     (if (stringp val)
  3555.     (let ((file (ange-ftp-expand-symlink val ange-ftp-this-dir)))
  3556.       (or (file-directory-p file)
  3557.           (and (file-exists-p file)
  3558.            (not (string-match ange-ftp-completion-ignored-pattern
  3559.                       symname)))))
  3560.       (or val ; is a directory name
  3561.       (not (string-match ange-ftp-completion-ignored-pattern symname))))))
  3562.  
  3563. (defun ange-ftp-file-name-all-completions (file dir)
  3564.   (let ((ange-ftp-this-dir (expand-file-name dir)))
  3565.     (if (ange-ftp-ftp-name ange-ftp-this-dir)
  3566.     (progn
  3567.       (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
  3568.       (setq ange-ftp-this-dir
  3569.         (ange-ftp-real-file-name-as-directory ange-ftp-this-dir))
  3570.       (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
  3571.          (completions
  3572.           (all-completions file tbl
  3573.                    (function ange-ftp-file-entry-active-p))))
  3574.  
  3575.         ;; see whether each matching file is a directory or not...
  3576.         (mapcar
  3577.          (function
  3578.           (lambda (file)
  3579.         (let ((ent (ange-ftp-get-hash-entry file tbl)))
  3580.           (if (and ent
  3581.                (or (not (stringp ent))
  3582.                    (file-directory-p
  3583.                 (ange-ftp-expand-symlink ent
  3584.                              ange-ftp-this-dir))))
  3585.               (concat file "/")
  3586.             file))))
  3587.          completions)))
  3588.  
  3589.       (if (string-equal "/" ange-ftp-this-dir)
  3590.       (nconc (all-completions file (ange-ftp-generate-root-prefixes))
  3591.          (ange-ftp-real-file-name-all-completions file
  3592.                               ange-ftp-this-dir))
  3593.     (ange-ftp-real-file-name-all-completions file ange-ftp-this-dir)))))
  3594.  
  3595. (defun ange-ftp-file-name-completion (file dir)
  3596.   (let ((ange-ftp-this-dir (expand-file-name dir)))
  3597.     (if (ange-ftp-ftp-name ange-ftp-this-dir)
  3598.     (progn
  3599.       (ange-ftp-barf-if-not-directory ange-ftp-this-dir)
  3600.       (if (equal file "")
  3601.           ""
  3602.         (setq ange-ftp-this-dir
  3603.           (ange-ftp-real-file-name-as-directory ange-ftp-this-dir))    ;real?
  3604.         (let* ((tbl (ange-ftp-get-files ange-ftp-this-dir))
  3605.            (ange-ftp-completion-ignored-pattern
  3606.             (mapconcat (function
  3607.                 (lambda (s) (if (stringp s)
  3608.                         (concat (regexp-quote s) "$")
  3609.                           "/"))) ; / never in filename
  3610.                    completion-ignored-extensions
  3611.                    "\\|")))
  3612.           (save-match-data
  3613.         (or (ange-ftp-file-name-completion-1
  3614.              file tbl ange-ftp-this-dir
  3615.              (function ange-ftp-file-entry-not-ignored-p))
  3616.             (ange-ftp-file-name-completion-1
  3617.              file tbl ange-ftp-this-dir
  3618.              (function ange-ftp-file-entry-active-p)))))))
  3619.  
  3620.       (if (string-equal "/" ange-ftp-this-dir)
  3621.       (try-completion
  3622.        file
  3623.        (nconc (ange-ftp-generate-root-prefixes)
  3624.           (mapcar 'list
  3625.               (ange-ftp-real-file-name-all-completions file "/"))))
  3626.     (ange-ftp-real-file-name-completion file ange-ftp-this-dir)))))
  3627.  
  3628.  
  3629. (defun ange-ftp-file-name-completion-1 (file tbl dir predicate)
  3630.   (let ((bestmatch (try-completion file tbl predicate)))
  3631.     (if bestmatch
  3632.     (if (eq bestmatch t)
  3633.         (if (file-directory-p (expand-file-name file dir))
  3634.         (concat file "/")
  3635.           t)
  3636.       (if (and (eq (try-completion bestmatch tbl predicate) t)
  3637.            (file-directory-p
  3638.             (expand-file-name bestmatch dir)))
  3639.           (concat bestmatch "/")
  3640.         bestmatch)))))
  3641.  
  3642. ;; Put these lines uncommmented in your .emacs if you want C-r to refresh
  3643. ;; ange-ftp's cache whilst doing filename completion.
  3644. ;;
  3645. ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir)
  3646. ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir)
  3647.  
  3648. ;; Force a re-read of the directory DIR.  If DIR is omitted then it defaults
  3649. ;; to the directory part of the contents of the current buffer.
  3650. (defun ange-ftp-re-read-dir (&optional dir)
  3651.   (interactive)
  3652.   (if dir
  3653.       (setq dir (expand-file-name dir))
  3654.     (setq dir (file-name-directory (expand-file-name (buffer-string)))))
  3655.   (if (ange-ftp-ftp-name dir)
  3656.       (progn
  3657.     (setq ange-ftp-ls-cache-file nil)
  3658.     (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable)
  3659.     (ange-ftp-get-files dir t))))
  3660.  
  3661. (defun ange-ftp-make-directory (dir &optional parents)
  3662.   (interactive (list (expand-file-name (read-file-name "Make directory: "))))
  3663.   (if parents
  3664.       (let ((parent (file-name-directory (directory-file-name dir))))
  3665.     (or (file-exists-p parent)
  3666.         (ange-ftp-make-directory parent parents))))
  3667.   (if (file-exists-p dir)
  3668.       (error "Cannot make directory %s: file already exists" dir)
  3669.     (let ((parsed (ange-ftp-ftp-name dir)))
  3670.       (if parsed
  3671.       (let* ((host (nth 0 parsed))
  3672.          (user (nth 1 parsed))
  3673.          ;; Some ftp's on unix machines (at least on Suns)
  3674.          ;; insist that mkdir take a filename, and not a
  3675.          ;; directory-name name as an arg. Argh!! This is a bug.
  3676.          ;; Non-unix machines will probably always insist
  3677.          ;; that mkdir takes a directory-name as an arg
  3678.          ;; (as the ftp man page says it should).
  3679.          (name (ange-ftp-quote-string
  3680.             (if (eq (ange-ftp-host-type host) 'unix)
  3681.                 (ange-ftp-real-directory-file-name (nth 2 parsed))
  3682.               (ange-ftp-real-file-name-as-directory
  3683.                (nth 2 parsed)))))
  3684.          (abbr (ange-ftp-abbreviate-filename dir))
  3685.          (result (ange-ftp-send-cmd host user
  3686.                         (list 'mkdir name)
  3687.                         (format "Making directory %s"
  3688.                             abbr))))
  3689.         (or (car result)
  3690.         (ange-ftp-error host user
  3691.                 (format "Could not make directory %s: %s"
  3692.                     dir
  3693.                     (cdr result))))
  3694.         (ange-ftp-add-file-entry dir t))
  3695.     (ange-ftp-real-make-directory dir)))))
  3696.  
  3697. (defun ange-ftp-delete-directory (dir)
  3698.   (if (file-directory-p dir)
  3699.       (let ((parsed (ange-ftp-ftp-name dir)))
  3700.     (if parsed
  3701.         (let* ((host (nth 0 parsed))
  3702.            (user (nth 1 parsed))
  3703.            ;; Some ftp's on unix machines (at least on Suns)
  3704.            ;; insist that rmdir take a filename, and not a
  3705.            ;; directory-name name as an arg. Argh!! This is a bug.
  3706.            ;; Non-unix machines will probably always insist
  3707.            ;; that rmdir takes a directory-name as an arg
  3708.            ;; (as the ftp man page says it should).
  3709.            (name (ange-ftp-quote-string
  3710.               (if (eq (ange-ftp-host-type host) 'unix)
  3711.                   (ange-ftp-real-directory-file-name
  3712.                    (nth 2 parsed))
  3713.                 (ange-ftp-real-file-name-as-directory
  3714.                  (nth 2 parsed)))))
  3715.           (abbr (ange-ftp-abbreviate-filename dir))
  3716.           (result (ange-ftp-send-cmd host user
  3717.                          (list 'rmdir name)
  3718.                          (format "Removing directory %s"
  3719.                              abbr))))
  3720.           (or (car result)
  3721.           (ange-ftp-error host user
  3722.                   (format "Could not remove directory %s: %s"
  3723.                       dir
  3724.                       (cdr result))))
  3725.           (ange-ftp-delete-file-entry dir t))
  3726.       (ange-ftp-real-delete-directory dir)))
  3727.     (error "Not a directory: %s" dir)))
  3728.  
  3729. ;; Make a local copy of FILE and return its name.
  3730.  
  3731. (defun ange-ftp-file-local-copy (file)
  3732.   (let* ((fn1 (expand-file-name file))
  3733.      (pa1 (ange-ftp-ftp-name fn1)))
  3734.     (if pa1
  3735.     (let ((tmp1 (ange-ftp-make-tmp-name (car pa1))))
  3736.       (ange-ftp-copy-file-internal fn1 tmp1 t nil
  3737.                        (format "Getting %s" fn1))
  3738.       tmp1))))
  3739.  
  3740. (defun ange-ftp-load (file &optional noerror nomessage nosuffix)
  3741.   (if (ange-ftp-ftp-name file)
  3742.       (let ((tryfiles (if nosuffix
  3743.               (list file)
  3744.             (list (concat file ".elc") (concat file ".el") file)))
  3745.         copy)
  3746.     (while (and tryfiles (not copy))
  3747.       (condition-case error
  3748.           (setq copy (ange-ftp-file-local-copy (car tryfiles)))
  3749.         (ftp-error nil))
  3750.       (setq tryfiles (cdr tryfiles)))
  3751.     (if copy
  3752.         (unwind-protect
  3753.         (funcall 'load copy noerror nomessage nosuffix)
  3754.           (delete-file copy))
  3755.       (or noerror
  3756.           (signal 'file-error (list "Cannot open load file" file)))))
  3757.     (ange-ftp-real-load file noerror nomessage nosuffix)))
  3758.  
  3759. ;; Calculate default-unhandled-directory for a given ange-ftp buffer.
  3760. (defun ange-ftp-unhandled-file-name-directory (filename)
  3761.   (file-name-directory ange-ftp-tmp-name-template))
  3762.  
  3763.  
  3764. ;; Need the following functions for making filenames of compressed
  3765. ;; files, because some OS's (unlike UNIX) do not allow a filename to
  3766. ;; have two extensions.
  3767.  
  3768. (defvar ange-ftp-make-compressed-filename-alist nil
  3769.   "Alist of host-type-specific functions to process file names for compression.
  3770. Each element has the form (TYPE . FUNC).
  3771. FUNC should take one argument, a file name, and return a list
  3772. of the form (COMPRESSING NEWNAME).
  3773. COMPRESSING should be t if the specified file should be compressed, 
  3774. and nil if it should be uncompressed (that is, if it is a compressed file).
  3775. NEWNAME should be the name to give the new compressed or uncompressed file.")
  3776.  
  3777. (defun ange-ftp-dired-compress-file (name)
  3778.   (let ((parsed (ange-ftp-ftp-name name))
  3779.     conversion-func)
  3780.     (if (and parsed
  3781.          (setq conversion-func
  3782.            (cdr (assq (ange-ftp-host-type (car parsed))
  3783.                   ange-ftp-make-compressed-filename-alist))))
  3784.     (let* ((decision
  3785.         (save-match-data (funcall conversion-func name)))
  3786.            (compressing (car decision))
  3787.            (newfile (nth 1 decision)))
  3788.       (if compressing
  3789.           (ange-ftp-compress name newfile)
  3790.         (ange-ftp-uncompress name newfile)))
  3791.       (let (file-name-handler-alist)
  3792.     (dired-compress-file name)))))
  3793.  
  3794. ;; Copy FILE to this machine, compress it, and copy out to NFILE.
  3795. (defun ange-ftp-compress (file nfile)
  3796.   (let* ((parsed (ange-ftp-ftp-name file))
  3797.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  3798.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  3799.      (abbr (ange-ftp-abbreviate-filename file))
  3800.      (nabbr (ange-ftp-abbreviate-filename nfile))
  3801.      (msg1 (format "Getting %s" abbr))
  3802.      (msg2 (format "Putting %s" nabbr)))
  3803.     (unwind-protect
  3804.     (progn
  3805.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  3806.       (and ange-ftp-process-verbose
  3807.            (ange-ftp-message "Compressing %s..." abbr))
  3808.       (call-process-region (point)
  3809.                    (point)
  3810.                    shell-file-name
  3811.                    nil
  3812.                    t
  3813.                    nil
  3814.                    "-c"
  3815.                    (format "compress -f -c < %s > %s" tmp1 tmp2))
  3816.       (and ange-ftp-process-verbose
  3817.            (ange-ftp-message "Compressing %s...done" abbr))
  3818.       (if (zerop (buffer-size))
  3819.           (progn
  3820.         (let (ange-ftp-process-verbose)
  3821.           (delete-file file))
  3822.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  3823.       (ange-ftp-del-tmp-name tmp1)
  3824.       (ange-ftp-del-tmp-name tmp2))))
  3825.     
  3826. ;; Copy FILE to this machine, uncompress it, and copy out to NFILE.
  3827. (defun ange-ftp-uncompress (file nfile)
  3828.   (let* ((parsed (ange-ftp-ftp-name file))
  3829.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  3830.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  3831.      (abbr (ange-ftp-abbreviate-filename file))
  3832.      (nabbr (ange-ftp-abbreviate-filename nfile))
  3833.      (msg1 (format "Getting %s" abbr))
  3834.      (msg2 (format "Putting %s" nabbr))
  3835. ;;     ;; Cheap hack because of problems with binary file transfers from
  3836. ;;     ;; VMS hosts.
  3837. ;;     (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed)))))
  3838.      )
  3839.     (unwind-protect
  3840.     (progn
  3841.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  3842.       (and ange-ftp-process-verbose
  3843.            (ange-ftp-message "Uncompressing %s..." abbr))
  3844.       (call-process-region (point)
  3845.                    (point)
  3846.                    shell-file-name
  3847.                    nil
  3848.                    t
  3849.                    nil
  3850.                    "-c"
  3851.                    (format "uncompress -c < %s > %s" tmp1 tmp2))
  3852.       (and ange-ftp-process-verbose
  3853.            (ange-ftp-message "Uncompressing %s...done" abbr))
  3854.       (if (zerop (buffer-size))
  3855.           (progn
  3856.         (let (ange-ftp-process-verbose)
  3857.           (delete-file file))
  3858.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  3859.       (ange-ftp-del-tmp-name tmp1)
  3860.       (ange-ftp-del-tmp-name tmp2))))
  3861.  
  3862. (defun ange-ftp-find-backup-file-name (fn)
  3863.   ;; Either return the ordinary backup name, etc.,
  3864.   ;; or return nil meaning don't make a backup.
  3865.   (if ange-ftp-make-backup-files
  3866.       (ange-ftp-real-find-backup-file-name fn)))
  3867.  
  3868. ;;; Define the handler for special file names
  3869. ;;; that causes ange-ftp to be invoked.
  3870.  
  3871. ;;;###autoload
  3872. (defun ange-ftp-hook-function (operation &rest args)
  3873.   (let ((fn (get operation 'ange-ftp)))
  3874.     (if fn (apply fn args)
  3875.       (ange-ftp-run-real-handler operation args))))
  3876.  
  3877.  
  3878. ;;; This regexp takes care of real ange-ftp file names (with a slash
  3879. ;;; and colon).
  3880. ;;; Don't allow the host name to end in a period--some systems use /.:
  3881. ;;;###autoload
  3882. (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist)
  3883.     (setq file-name-handler-alist
  3884.       (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function)
  3885.         file-name-handler-alist)))
  3886.  
  3887. ;;; This regexp recognizes and absolute filenames with only one component,
  3888. ;;; for the sake of hostname completion.
  3889. ;;;###autoload
  3890. (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
  3891.     (setq file-name-handler-alist
  3892.       (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
  3893.         file-name-handler-alist)))
  3894.  
  3895. ;;; The above two forms are sufficient to cause this file to be loaded
  3896. ;;; if the user ever uses a file name with a colon in it.
  3897.  
  3898. ;;; This sets the mode 
  3899. (or (memq 'ange-ftp-set-buffer-mode find-file-hooks)
  3900.     (setq find-file-hooks
  3901.       (cons 'ange-ftp-set-buffer-mode find-file-hooks)))
  3902.  
  3903. ;;; Now say where to find the handlers for particular operations.
  3904.  
  3905. (put 'file-name-directory 'ange-ftp 'ange-ftp-file-name-directory)
  3906. (put 'file-name-nondirectory 'ange-ftp 'ange-ftp-file-name-nondirectory)
  3907. (put 'file-name-as-directory 'ange-ftp 'ange-ftp-file-name-as-directory)
  3908. (put 'directory-file-name 'ange-ftp 'ange-ftp-directory-file-name)
  3909. (put 'expand-file-name 'ange-ftp 'ange-ftp-expand-file-name)
  3910. (put 'make-directory 'ange-ftp 'ange-ftp-make-directory)
  3911. (put 'delete-directory 'ange-ftp 'ange-ftp-delete-directory)
  3912. (put 'insert-file-contents 'ange-ftp 'ange-ftp-insert-file-contents)
  3913. (put 'directory-files 'ange-ftp 'ange-ftp-directory-files)
  3914. (put 'file-directory-p 'ange-ftp 'ange-ftp-file-directory-p)
  3915. (put 'file-writable-p 'ange-ftp 'ange-ftp-file-writable-p)
  3916. (put 'file-readable-p 'ange-ftp 'ange-ftp-file-readable-p)
  3917. (put 'file-executable-p 'ange-ftp 'ange-ftp-file-executable-p)
  3918. (put 'file-symlink-p 'ange-ftp 'ange-ftp-file-symlink-p)
  3919. (put 'delete-file 'ange-ftp 'ange-ftp-delete-file)
  3920. (put 'read-file-name-internal 'ange-ftp 'ange-ftp-read-file-name-internal)
  3921. (put 'verify-visited-file-modtime 'ange-ftp
  3922.      'ange-ftp-verify-visited-file-modtime)
  3923. (put 'file-exists-p 'ange-ftp 'ange-ftp-file-exists-p)
  3924. (put 'write-region 'ange-ftp 'ange-ftp-write-region)
  3925. (put 'backup-buffer 'ange-ftp 'ange-ftp-backup-buffer)
  3926. (put 'copy-file 'ange-ftp 'ange-ftp-copy-file)
  3927. (put 'rename-file 'ange-ftp 'ange-ftp-rename-file)
  3928. (put 'file-attributes 'ange-ftp 'ange-ftp-file-attributes)
  3929. (put 'file-name-all-completions 'ange-ftp 'ange-ftp-file-name-all-completions)
  3930. (put 'file-name-completion 'ange-ftp 'ange-ftp-file-name-completion)
  3931. (put 'insert-directory 'ange-ftp 'ange-ftp-insert-directory)
  3932. (put 'file-local-copy 'ange-ftp 'ange-ftp-file-local-copy)
  3933. (put 'unhandled-file-name-directory 'ange-ftp
  3934.      'ange-ftp-unhandled-file-name-directory)
  3935. (put 'file-name-sans-versions 'ange-ftp 'ange-ftp-file-name-sans-versions)
  3936. (put 'dired-uncache 'ange-ftp 'ange-ftp-dired-uncache)
  3937. (put 'dired-compress-file 'ange-ftp 'ange-ftp-dired-compress-file)
  3938. (put 'load 'ange-ftp 'ange-ftp-load)
  3939. (put 'find-backup-file-name 'ange-ftp 'ange-ftp-find-backup-file-name)
  3940.  
  3941. ;; Turn off truename processing to save time.
  3942. ;; Treat each name as its own truename.
  3943. (put 'file-truename 'ange-ftp 'identity)
  3944.  
  3945. ;; Turn off RCS/SCCS processing to save time.
  3946. ;; This returns nil for any file name as argument.
  3947. (put 'vc-registered 'ange-ftp 'null)
  3948.  
  3949. (put 'dired-call-process 'ange-ftp 'ange-ftp-dired-call-process)
  3950.  
  3951. ;;; Define ways of getting at unmodified Emacs primitives,
  3952. ;;; turning off our handler.
  3953.  
  3954. (defun ange-ftp-run-real-handler (operation args)
  3955.   (let ((inhibit-file-name-handlers
  3956.      (cons 'ange-ftp-hook-function
  3957.            (cons 'ange-ftp-completion-hook-function
  3958.              (and (eq inhibit-file-name-operation operation)
  3959.               inhibit-file-name-handlers))))
  3960.     (inhibit-file-name-operation operation))
  3961.     (apply operation args)))
  3962.  
  3963. (defun ange-ftp-remote-expand-file-name (name &optional default)
  3964.   (let ((remote-expand-file-name t))
  3965.     (ange-ftp-real-expand-file-name name default)))
  3966.  
  3967. (defun ange-ftp-real-file-name-directory (&rest args)
  3968.   (ange-ftp-run-real-handler 'file-name-directory args))
  3969. (defun ange-ftp-real-file-name-nondirectory (&rest args)
  3970.   (ange-ftp-run-real-handler 'file-name-nondirectory args))
  3971. (defun ange-ftp-real-file-name-as-directory (&rest args)
  3972.   (ange-ftp-run-real-handler 'file-name-as-directory args))
  3973. (defun ange-ftp-real-directory-file-name (&rest args)
  3974.   (ange-ftp-run-real-handler 'directory-file-name args))
  3975. (defun ange-ftp-real-expand-file-name (&rest args)
  3976.   (ange-ftp-run-real-handler 'expand-file-name args))
  3977. (defun ange-ftp-real-make-directory (&rest args)
  3978.   (ange-ftp-run-real-handler 'make-directory args))
  3979. (defun ange-ftp-real-delete-directory (&rest args)
  3980.   (ange-ftp-run-real-handler 'delete-directory args))
  3981. (defun ange-ftp-real-insert-file-contents (&rest args)
  3982.   (ange-ftp-run-real-handler 'insert-file-contents args))
  3983. (defun ange-ftp-real-directory-files (&rest args)
  3984.   (ange-ftp-run-real-handler 'directory-files args))
  3985. (defun ange-ftp-real-file-directory-p (&rest args)
  3986.   (ange-ftp-run-real-handler 'file-directory-p args))
  3987. (defun ange-ftp-real-file-writable-p (&rest args)
  3988.   (ange-ftp-run-real-handler 'file-writable-p args))
  3989. (defun ange-ftp-real-file-readable-p (&rest args)
  3990.   (ange-ftp-run-real-handler 'file-readable-p args))
  3991. (defun ange-ftp-real-file-executable-p (&rest args)
  3992.   (ange-ftp-run-real-handler 'file-executable-p args))
  3993. (defun ange-ftp-real-file-symlink-p (&rest args)
  3994.   (ange-ftp-run-real-handler 'file-symlink-p args))
  3995. (defun ange-ftp-real-delete-file (&rest args)
  3996.   (ange-ftp-run-real-handler 'delete-file args))
  3997. (defun ange-ftp-real-read-file-name-internal (&rest args)
  3998.   (ange-ftp-run-real-handler 'read-file-name-internal args))
  3999. (defun ange-ftp-real-verify-visited-file-modtime (&rest args)
  4000.   (ange-ftp-run-real-handler 'verify-visited-file-modtime args))
  4001. (defun ange-ftp-real-file-exists-p (&rest args)
  4002.   (ange-ftp-run-real-handler 'file-exists-p args))
  4003. (defun ange-ftp-real-write-region (&rest args)
  4004.   (ange-ftp-run-real-handler 'write-region args))
  4005. (defun ange-ftp-real-backup-buffer (&rest args)
  4006.   (ange-ftp-run-real-handler 'backup-buffer args))
  4007. (defun ange-ftp-real-copy-file (&rest args)
  4008.   (ange-ftp-run-real-handler 'copy-file args))
  4009. (defun ange-ftp-real-rename-file (&rest args)
  4010.   (ange-ftp-run-real-handler 'rename-file args))
  4011. (defun ange-ftp-real-file-attributes (&rest args)
  4012.   (ange-ftp-run-real-handler 'file-attributes args))
  4013. (defun ange-ftp-real-file-name-all-completions (&rest args)
  4014.   (ange-ftp-run-real-handler 'file-name-all-completions args))
  4015. (defun ange-ftp-real-file-name-completion (&rest args)
  4016.   (ange-ftp-run-real-handler 'file-name-completion args))
  4017. (defun ange-ftp-real-insert-directory (&rest args)
  4018.   (ange-ftp-run-real-handler 'insert-directory args))
  4019. (defun ange-ftp-real-file-name-sans-versions (&rest args)
  4020.   (ange-ftp-run-real-handler 'file-name-sans-versions args))
  4021. (defun ange-ftp-real-shell-command (&rest args)
  4022.   (ange-ftp-run-real-handler 'shell-command args))
  4023. (defun ange-ftp-real-load (&rest args)
  4024.   (ange-ftp-run-real-handler 'load args))
  4025. (defun ange-ftp-real-find-backup-file-name (&rest args)
  4026.   (ange-ftp-run-real-handler 'find-backup-file-name args))
  4027.  
  4028. ;; Here we support using dired on remote hosts.
  4029. ;; I have turned off the support for using dired on foreign directory formats.
  4030. ;; That involves too many unclean hooks.
  4031. ;; It would be cleaner to support such operations by 
  4032. ;; converting the foreign directory format to something dired can understand;
  4033. ;; something close to ls -l output.
  4034. ;; The logical place to do this is in the functions ange-ftp-parse-...-listing.
  4035.  
  4036. ;; Some of the old dired hooks would still be needed even if this is done.
  4037. ;; I have preserved (and modernized) those hooks.
  4038. ;; So the format conversion should be all that is needed.
  4039.  
  4040. (defun ange-ftp-insert-directory (file switches &optional wildcard full)
  4041.   (let ((short (ange-ftp-abbreviate-filename file))
  4042.     (parsed (ange-ftp-ftp-name (expand-file-name file))))
  4043.     (if parsed
  4044.     (insert
  4045.      (if wildcard
  4046.          (let ((default-directory (file-name-directory file)))
  4047.            (ange-ftp-ls (file-name-nondirectory file) switches nil nil t))
  4048.        (ange-ftp-ls file switches full)))
  4049.       (ange-ftp-real-insert-directory file switches wildcard full))))
  4050.  
  4051. (defun ange-ftp-dired-uncache (dir)
  4052.   (if (ange-ftp-ftp-name (expand-file-name dir))
  4053.       (setq ange-ftp-ls-cache-file nil)))
  4054.  
  4055. (defvar ange-ftp-sans-version-alist nil
  4056.   "Alist of mapping host type into function to remove file version numbers.")
  4057.  
  4058. (defun ange-ftp-file-name-sans-versions (file keep-backup-version)
  4059.   (setq file (ange-ftp-abbreviate-filename file))
  4060.   (let ((parsed (ange-ftp-ftp-name file))
  4061.     host-type func)
  4062.     (if parsed
  4063.     (setq host-type (ange-ftp-host-type (car parsed))
  4064.           func (cdr (assq (ange-ftp-host-type (car parsed))
  4065.                   ange-ftp-sans-version-alist))))
  4066.     (if func (funcall func file keep-backup-version)
  4067.       (ange-ftp-real-file-name-sans-versions file keep-backup-version))))
  4068.  
  4069. ;;; This doesn't work yet; a new hook needs to be created.
  4070. ;;; Maybe the new hook should be in call-process.
  4071. (defun ange-ftp-shell-command (command)
  4072.   (let* ((parsed (ange-ftp-ftp-name default-directory))
  4073.      (host (nth 0 parsed))
  4074.      (user (nth 1 parsed))
  4075.      (name (nth 2 parsed)))
  4076.     (if (not parsed)
  4077.     (ange-ftp-real-shell-command command)
  4078.       (if (> (length name) 0)        ; else it's $HOME
  4079.       (setq command (concat "cd " name "; " command)))
  4080.       (setq command
  4081.         (format  "%s %s \"%s\""    ; remsh -l USER does not work well
  4082.                     ; on a hp-ux machine I tried
  4083.              remote-shell-program host command))
  4084.       (ange-ftp-message "Remote command '%s' ..." command)
  4085.       ;; Cannot call ange-ftp-real-dired-run-shell-command here as it
  4086.       ;; would prepend "cd default-directory" --- which bombs because
  4087.       ;; default-directory is in ange-ftp syntax for remote file names.
  4088.       (ange-ftp-real-shell-command command))))
  4089.  
  4090. ;;; This is the handler for call-process.
  4091. (defun ange-ftp-dired-call-process (program discard &rest arguments)
  4092.   ;; PROGRAM is always one of those below in the cond in dired.el.
  4093.   ;; The ARGUMENTS are (nearly) always files.
  4094.   (if (ange-ftp-ftp-name default-directory)
  4095.       ;; Can't use ange-ftp-dired-host-type here because the current
  4096.       ;; buffer is *dired-check-process output*
  4097.       (condition-case oops
  4098.       (cond ((equal "chmod" program)
  4099.          (ange-ftp-call-chmod arguments))
  4100.         ;; ((equal "chgrp" program))
  4101.         ;; ((equal dired-chown-program program))
  4102.         (t (error "Unknown remote command: %s" program)))
  4103.     (ftp-error (insert (format "%s: %s, %s\n"
  4104.                     (nth 1 oops)
  4105.                     (nth 2 oops)
  4106.                     (nth 3 oops)))
  4107.            ;; Caller expects nonzero value to mean failure.
  4108.            1)
  4109.     (error (insert (format "%s\n" (nth 1 oops)))
  4110.            1))
  4111.     (apply 'call-process program nil (not discard) nil arguments)))
  4112.  
  4113. (defvar ange-ftp-remote-shell "rsh" 
  4114.   "Remote shell to use for chmod, if FTP server rejects the `chmod' command.")
  4115.  
  4116. ;; Handle an attempt to run chmod on a remote file
  4117. ;; by using the ftp chmod command.
  4118. (defun ange-ftp-call-chmod (args)
  4119.   (if (< (length args) 2)
  4120.       (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args))
  4121.   (let ((mode (car args)))
  4122.     (mapcar
  4123.      (function
  4124.       (lambda (file)
  4125.     (setq file (expand-file-name file))
  4126.     (let ((parsed (ange-ftp-ftp-name file)))
  4127.       (if parsed
  4128.           (let* ((host (nth 0 parsed))
  4129.              (user (nth 1 parsed))
  4130.              (name (ange-ftp-quote-string (nth 2 parsed)))
  4131.              (abbr (ange-ftp-abbreviate-filename file))
  4132.              (result (ange-ftp-send-cmd host user
  4133.                         (list 'chmod mode name)
  4134.                         (format "doing chmod %s"
  4135.                             abbr))))
  4136.         (or (car result)
  4137.             (call-process 
  4138.              ange-ftp-remote-shell
  4139.              nil t nil host "chmod" mode name)))))))
  4140.      (cdr args)))
  4141.   (setq ange-ftp-ls-cache-file nil)    ;Stop confusing Dired.
  4142.   0)
  4143.  
  4144. ;;; This is turned off because it has nothing properly to do
  4145. ;;; with dired.  It could be reasonable to adapt this to
  4146. ;;; replace ange-ftp-copy-file.
  4147.  
  4148. ;;;;; ------------------------------------------------------------
  4149. ;;;;; Noddy support for async copy-file within dired.
  4150. ;;;;; ------------------------------------------------------------
  4151.  
  4152. ;;(defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait)
  4153. ;;  "Documented as original."
  4154. ;;  (dired-handle-overwrite to)
  4155. ;;  (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil
  4156. ;;                   cont nowait))
  4157.  
  4158. ;;(defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg
  4159. ;;                         &optional marker-char op1
  4160. ;;                         how-to)
  4161. ;;  "Documented as original."
  4162. ;;  ;; we need to let ange-ftp-dired-create-files know that we indirectly
  4163. ;;  ;; called it rather than somebody else.
  4164. ;;  (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is
  4165. ;;    (ange-ftp-real-dired-do-create-files op-symbol file-creator operation
  4166. ;;                     arg marker-char op1 how-to)))
  4167.  
  4168. ;;(defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor
  4169. ;;                         &optional marker-char)
  4170. ;;  "Documented as original."
  4171. ;;  (if (and (boundp 'ange-ftp-dired-do-create-files)
  4172. ;;       ;; called from ange-ftp-dired-do-create-files?
  4173. ;;       ange-ftp-dired-do-create-files
  4174. ;;       ;; any files worth copying?
  4175. ;;       fn-list
  4176. ;;       ;; we only support async copy-file at the mo.
  4177. ;;       (eq file-creator 'dired-copy-file)
  4178. ;;       ;; it is only worth calling the alternative function for remote files
  4179. ;;       ;; as we tie ourself in recursive knots otherwise.
  4180. ;;       (or (ange-ftp-ftp-name (car fn-list))
  4181. ;;           ;; we can only call the name constructor for dired-do-create-files
  4182. ;;           ;; since the one for regexps starts prompting here, there and
  4183. ;;           ;; everywhere.
  4184. ;;           (ange-ftp-ftp-name (funcall name-constructor (car fn-list)))))
  4185. ;;      ;; use the process-filter driven routine rather than the iterative one.
  4186. ;;      (ange-ftp-dcf-1 file-creator
  4187. ;;                 operation
  4188. ;;               fn-list
  4189. ;;               name-constructor
  4190. ;;               (and (boundp 'target) target)    ;dynamically bound
  4191. ;;               marker-char
  4192. ;;               (current-buffer)
  4193. ;;               nil    ;overwrite-query
  4194. ;;               nil    ;overwrite-backup-query
  4195. ;;               nil    ;failures
  4196. ;;               nil    ;skipped
  4197. ;;               0        ;success-count
  4198. ;;               (length fn-list) ;total
  4199. ;;               )
  4200. ;;    ;; normal case... use the interactive routine... much cheaper.
  4201. ;;    (ange-ftp-real-dired-create-files file-creator operation fn-list
  4202. ;;                      name-constructor marker-char)))
  4203.  
  4204. ;;(defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor
  4205. ;;               target marker-char buffer overwrite-query 
  4206. ;;               overwrite-backup-query failures skipped
  4207. ;;               success-count total)
  4208. ;;  (let ((old-buf (current-buffer)))
  4209. ;;    (unwind-protect
  4210. ;;    (progn
  4211. ;;      (set-buffer buffer)
  4212. ;;      (if (null fn-list)
  4213. ;;          (ange-ftp-dcf-3 failures operation total skipped
  4214. ;;                  success-count buffer)
  4215.         
  4216. ;;        (let* ((from (car fn-list))
  4217. ;;           (to (funcall name-constructor from)))
  4218. ;;          (if (equal to from)
  4219. ;;          (progn
  4220. ;;            (setq to nil)
  4221. ;;            (dired-log "Cannot %s to same file: %s\n"
  4222. ;;                   (downcase operation) from)))
  4223. ;;          (if (not to)
  4224. ;;          (ange-ftp-dcf-1 file-creator
  4225. ;;                  operation
  4226. ;;                  (cdr fn-list)
  4227. ;;                  name-constructor
  4228. ;;                  target
  4229. ;;                  marker-char
  4230. ;;                  buffer
  4231. ;;                  overwrite-query
  4232. ;;                  overwrite-backup-query
  4233. ;;                  failures
  4234. ;;                  (cons (dired-make-relative from) skipped)
  4235. ;;                  success-count
  4236. ;;                  total)
  4237. ;;        (let* ((overwrite (file-exists-p to))
  4238. ;;               (overwrite-confirmed    ; for dired-handle-overwrite
  4239. ;;            (and overwrite
  4240. ;;                 (let ((help-form '(format "\
  4241. ;;Type SPC or `y' to overwrite file `%s',
  4242. ;;DEL or `n' to skip to next,
  4243. ;;ESC or `q' to not overwrite any of the remaining files,
  4244. ;;`!' to overwrite all remaining files with no more questions." to)))
  4245. ;;                   (dired-query 'overwrite-query
  4246. ;;                        "Overwrite `%s'?" to))))
  4247. ;;               ;; must determine if FROM is marked before file-creator
  4248. ;;               ;; gets a chance to delete it (in case of a move).
  4249. ;;               (actual-marker-char
  4250. ;;            (cond  ((integerp marker-char) marker-char)
  4251. ;;                   (marker-char (dired-file-marker from)) ; slow
  4252. ;;                   (t nil))))
  4253. ;;          (condition-case err
  4254. ;;              (funcall file-creator from to overwrite-confirmed
  4255. ;;                   (list (function ange-ftp-dcf-2)
  4256. ;;                     nil    ;err
  4257. ;;                     file-creator operation fn-list
  4258. ;;                     name-constructor
  4259. ;;                     target
  4260. ;;                     marker-char actual-marker-char
  4261. ;;                     buffer to from
  4262. ;;                     overwrite
  4263. ;;                     overwrite-confirmed
  4264. ;;                     overwrite-query 
  4265. ;;                     overwrite-backup-query
  4266. ;;                     failures skipped success-count
  4267. ;;                     total)
  4268. ;;                   t)
  4269. ;;            (file-error        ; FILE-CREATOR aborted
  4270. ;;             (ange-ftp-dcf-2 nil ;result
  4271. ;;                     nil ;line
  4272. ;;                     err
  4273. ;;                     file-creator operation fn-list
  4274. ;;                     name-constructor
  4275. ;;                     target
  4276. ;;                     marker-char actual-marker-char
  4277. ;;                     buffer to from
  4278. ;;                     overwrite
  4279. ;;                     overwrite-confirmed
  4280. ;;                     overwrite-query 
  4281. ;;                     overwrite-backup-query
  4282. ;;                     failures skipped success-count
  4283. ;;                     total))))))))
  4284. ;;      (set-buffer old-buf))))
  4285.  
  4286. ;;(defun ange-ftp-dcf-2 (result line err
  4287. ;;                  file-creator operation fn-list
  4288. ;;                  name-constructor
  4289. ;;                  target
  4290. ;;                  marker-char actual-marker-char
  4291. ;;                  buffer to from
  4292. ;;                  overwrite
  4293. ;;                  overwrite-confirmed
  4294. ;;                  overwrite-query 
  4295. ;;                  overwrite-backup-query
  4296. ;;                  failures skipped success-count
  4297. ;;                  total)
  4298. ;;  (let ((old-buf (current-buffer)))
  4299. ;;    (unwind-protect
  4300. ;;    (progn
  4301. ;;      (set-buffer buffer)
  4302. ;;      (if (or err (not result))
  4303. ;;          (progn
  4304. ;;        (setq failures (cons (dired-make-relative from) failures))
  4305. ;;        (dired-log "%s `%s' to `%s' failed:\n%s\n"
  4306. ;;               operation from to (or err line)))
  4307. ;;        (if overwrite
  4308. ;;        ;; If we get here, file-creator hasn't been aborted
  4309. ;;        ;; and the old entry (if any) has to be deleted
  4310. ;;        ;; before adding the new entry.
  4311. ;;        (dired-remove-file to))
  4312. ;;        (setq success-count (1+ success-count))
  4313. ;;        (message "%s: %d of %d" operation success-count total)
  4314. ;;        (dired-add-file to actual-marker-char))
  4315.       
  4316. ;;      (ange-ftp-dcf-1 file-creator operation (cdr fn-list)
  4317. ;;              name-constructor
  4318. ;;              target
  4319. ;;              marker-char
  4320. ;;              buffer
  4321. ;;              overwrite-query 
  4322. ;;              overwrite-backup-query
  4323. ;;              failures skipped success-count
  4324. ;;              total))
  4325. ;;      (set-buffer old-buf))))
  4326.  
  4327. ;;(defun ange-ftp-dcf-3 (failures operation total skipped success-count
  4328. ;;                buffer)
  4329. ;;  (let ((old-buf (current-buffer)))
  4330. ;;    (unwind-protect
  4331. ;;    (progn
  4332. ;;      (set-buffer buffer)
  4333. ;;      (cond
  4334. ;;       (failures
  4335. ;;        (dired-log-summary
  4336. ;;         (message "%s failed for %d of %d file%s %s"
  4337. ;;              operation (length failures) total
  4338. ;;              (dired-plural-s total) failures)))
  4339. ;;       (skipped
  4340. ;;        (dired-log-summary
  4341. ;;         (message "%s: %d of %d file%s skipped %s"
  4342. ;;              operation (length skipped) total
  4343. ;;              (dired-plural-s total) skipped)))
  4344. ;;       (t
  4345. ;;        (message "%s: %s file%s."
  4346. ;;             operation success-count (dired-plural-s success-count))))
  4347. ;;      (dired-move-to-filename))
  4348. ;;      (set-buffer old-buf))))
  4349.  
  4350. ;;;; -----------------------------------------------
  4351. ;;;; Unix Descriptive Listing (dl) Support
  4352. ;;;; -----------------------------------------------
  4353.  
  4354. ;; This is turned off because nothing uses it currently
  4355. ;; and because I don't understand what it's supposed to be for. --rms.
  4356.  
  4357. ;;(defconst ange-ftp-dired-dl-re-dir
  4358. ;;  "^. [^ /]+/[ \n]"
  4359. ;;  "Regular expression to use to search for dl directories.")
  4360.  
  4361. ;;(or (assq 'unix:dl ange-ftp-dired-re-dir-alist)
  4362. ;;    (setq ange-ftp-dired-re-dir-alist
  4363. ;;      (cons (cons 'unix:dl  ange-ftp-dired-dl-re-dir)
  4364. ;;        ange-ftp-dired-re-dir-alist)))
  4365.  
  4366. ;;(defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol)
  4367. ;;  "In dired, move to the first character of the filename on this line."
  4368. ;;  ;; This is the Unix dl version.
  4369. ;;  (or eol (setq eol (progn (end-of-line) (point))))
  4370. ;;  (let (case-fold-search)
  4371. ;;    (beginning-of-line)
  4372. ;;    (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ")
  4373. ;;    (goto-char (+ (point) 2))
  4374. ;;      (if raise-error
  4375. ;;      (error "No file on this line")
  4376. ;;    nil))))
  4377.  
  4378. ;;(or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist)
  4379. ;;    (setq ange-ftp-dired-move-to-filename-alist
  4380. ;;      (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename)
  4381. ;;        ange-ftp-dired-move-to-filename-alist)))
  4382.  
  4383. ;;(defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol)
  4384. ;;  ;; Assumes point is at beginning of filename.
  4385. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  4386. ;;  ;; On failure, signals an error or returns nil.
  4387. ;;  ;; This is the Unix dl version.
  4388. ;;  (let ((opoint (point))
  4389. ;;    case-fold-search hidden)
  4390. ;;    (or eol (setq eol (save-excursion (end-of-line) (point))))
  4391. ;;    (setq hidden (and selective-display
  4392. ;;               (save-excursion
  4393. ;;             (search-forward "\r" eol t))))
  4394. ;;    (if hidden
  4395. ;;    (if no-error
  4396. ;;        nil
  4397. ;;      (error
  4398. ;;       (substitute-command-keys
  4399. ;;        "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  4400. ;;      (skip-chars-forward "^ /" eol)
  4401. ;;      (if (eq opoint (point))
  4402. ;;      (if no-error
  4403. ;;          nil
  4404. ;;        (error "No file on this line"))
  4405. ;;    (point)))))
  4406.  
  4407. ;;(or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist)
  4408. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  4409. ;;      (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename)
  4410. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  4411.  
  4412. ;;;; ------------------------------------------------------------
  4413. ;;;; VOS support (VOS support is probably broken,
  4414. ;;;; but I don't know anything about VOS.)
  4415. ;;;; ------------------------------------------------------------
  4416. ;
  4417. ;(defun ange-ftp-fix-name-for-vos (name &optional reverse)
  4418. ;  (setq name (copy-sequence name))
  4419. ;  (let ((from (if reverse ?\> ?\/))
  4420. ;    (to (if reverse ?\/ ?\>))
  4421. ;    (i (1- (length name))))
  4422. ;    (while (>= i 0)
  4423. ;      (if (= (aref name i) from)
  4424. ;      (aset name i to))
  4425. ;      (setq i (1- i)))
  4426. ;    name))
  4427. ;
  4428. ;(or (assq 'vos ange-ftp-fix-name-func-alist)
  4429. ;    (setq ange-ftp-fix-name-func-alist
  4430. ;      (cons '(vos . ange-ftp-fix-name-for-vos)
  4431. ;        ange-ftp-fix-name-func-alist)))
  4432. ;
  4433. ;(or (memq 'vos ange-ftp-dumb-host-types)
  4434. ;    (setq ange-ftp-dumb-host-types
  4435. ;      (cons 'vos ange-ftp-dumb-host-types)))
  4436. ;
  4437. ;(defun ange-ftp-fix-dir-name-for-vos (dir-name)
  4438. ;  (ange-ftp-fix-name-for-vos
  4439. ;   (concat dir-name
  4440. ;       (if (eq ?/ (aref dir-name (1- (length dir-name))))
  4441. ;           "" "/")
  4442. ;       "*")))
  4443. ;
  4444. ;(or (assq 'vos ange-ftp-fix-dir-name-func-alist)
  4445. ;    (setq ange-ftp-fix-dir-name-func-alist
  4446. ;      (cons '(vos . ange-ftp-fix-dir-name-for-vos)
  4447. ;        ange-ftp-fix-dir-name-func-alist)))
  4448. ;
  4449. ;(defvar ange-ftp-vos-host-regexp nil
  4450. ;  "If a host matches this regexp then it is assumed to be running VOS.")
  4451. ;
  4452. ;(defun ange-ftp-vos-host (host)
  4453. ;  (and ange-ftp-vos-host-regexp
  4454. ;       (save-match-data
  4455. ;     (string-match ange-ftp-vos-host-regexp host))))
  4456. ;
  4457. ;(defun ange-ftp-parse-vos-listing ()
  4458. ;  "Parse the current buffer which is assumed to be in VOS list -all
  4459. ;format, and return a hashtable as the result."
  4460. ;  (let ((tbl (ange-ftp-make-hashtable))
  4461. ;    (type-list
  4462. ;     '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40)
  4463. ;       ("^Dirs: [0-9]+\n+" t 30)))
  4464. ;    type-regexp type-is-dir type-col file)
  4465. ;    (goto-char (point-min))
  4466. ;    (save-match-data
  4467. ;      (while type-list
  4468. ;    (setq type-regexp (car (car type-list))
  4469. ;          type-is-dir (nth 1 (car type-list))
  4470. ;          type-col (nth 2 (car type-list))
  4471. ;          type-list (cdr type-list))
  4472. ;    (if (re-search-forward type-regexp nil t)
  4473. ;        (while (eq (char-after (point)) ? )
  4474. ;          (move-to-column type-col)
  4475. ;          (setq file (buffer-substring (point)
  4476. ;                       (progn
  4477. ;                         (end-of-line 1)
  4478. ;                         (point))))
  4479. ;          (ange-ftp-put-hash-entry file type-is-dir tbl)
  4480. ;          (forward-line 1))))
  4481. ;      (ange-ftp-put-hash-entry "." 'vosdir tbl)
  4482. ;      (ange-ftp-put-hash-entry ".." 'vosdir tbl))
  4483. ;    tbl))
  4484. ;
  4485. ;(or (assq 'vos ange-ftp-parse-list-func-alist)
  4486. ;    (setq ange-ftp-parse-list-func-alist
  4487. ;      (cons '(vos . ange-ftp-parse-vos-listing)
  4488. ;        ange-ftp-parse-list-func-alist)))
  4489.  
  4490. ;;;; ------------------------------------------------------------
  4491. ;;;; VMS support.
  4492. ;;;; ------------------------------------------------------------
  4493.  
  4494. ;; Convert NAME from UNIX-ish to VMS.  If REVERSE given then convert from VMS
  4495. ;; to UNIX-ish.
  4496. (defun ange-ftp-fix-name-for-vms (name &optional reverse)
  4497.   (save-match-data
  4498.     (if reverse
  4499.     (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" name)
  4500.         (let (drive dir file)
  4501.           (if (match-beginning 1)
  4502.           (setq drive (substring name
  4503.                      (match-beginning 1)
  4504.                      (match-end 1))))
  4505.           (if (match-beginning 2)
  4506.           (setq dir
  4507.             (substring name (match-beginning 2) (match-end 2))))
  4508.           (if (match-beginning 3)
  4509.           (setq file
  4510.             (substring name (match-beginning 3) (match-end 3))))
  4511.           (and dir
  4512.            (setq dir (apply (function concat)
  4513.                     (mapcar (function
  4514.                          (lambda (char)
  4515.                            (if (= char ?.)
  4516.                            (vector ?/)
  4517.                          (vector char))))
  4518.                         (substring dir 1 -1)))))
  4519.           (concat (and drive
  4520.                (concat "/" drive "/"))
  4521.               dir (and dir "/")
  4522.               file))
  4523.       (error "name %s didn't match" name))
  4524.       (let (drive dir file tmp)
  4525.     (if (string-match "^/[^:]+:/" name)
  4526.         (setq drive (substring name 1
  4527.                    (1- (match-end 0)))
  4528.           name (substring name (match-end 0))))
  4529.     (setq tmp (file-name-directory name))
  4530.     (if tmp
  4531.         (setq dir (apply (function concat)
  4532.                  (mapcar (function
  4533.                       (lambda (char)
  4534.                     (if (= char ?/)
  4535.                         (vector ?.)
  4536.                       (vector char))))
  4537.                      (substring tmp 0 -1)))))
  4538.     (setq file (file-name-nondirectory name))
  4539.     (concat drive
  4540.         (and dir (concat "[" (if drive nil ".") dir "]"))
  4541.         file)))))
  4542.  
  4543. ;; (ange-ftp-fix-name-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1")
  4544. ;; (ange-ftp-fix-name-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t)
  4545.  
  4546. (or (assq 'vms ange-ftp-fix-name-func-alist)
  4547.     (setq ange-ftp-fix-name-func-alist
  4548.       (cons '(vms . ange-ftp-fix-name-for-vms)
  4549.         ange-ftp-fix-name-func-alist)))
  4550.  
  4551. (or (memq 'vms ange-ftp-dumb-host-types)
  4552.     (setq ange-ftp-dumb-host-types
  4553.       (cons 'vms ange-ftp-dumb-host-types)))
  4554.  
  4555. ;; It is important that this function barf for directories for which we know
  4556. ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/".
  4557. ;; This is because it saves an unnecessary FTP error, or possibly the listing
  4558. ;; might succeed, but give erroneous info. This last case is particularly
  4559. ;; likely for OS's (like MTS) for which we need to use a wildcard in order
  4560. ;; to list a directory.
  4561.  
  4562. ;; Convert name from UNIX-ish to VMS ready for a DIRectory listing.
  4563. (defun ange-ftp-fix-dir-name-for-vms (dir-name)
  4564.   ;; Should there be entries for .. -> [-] and . -> [] below. Don't
  4565.   ;; think so, because expand-filename should have already short-circuited
  4566.   ;; them.
  4567.   (cond ((string-equal dir-name "/")
  4568.      (error "Cannot get listing for fictitious \"/\" directory."))
  4569.     ((string-match "^/[-A-Z0-9_$]+:/$" dir-name)
  4570.      (error "Cannot get listing for device."))
  4571.     ((ange-ftp-fix-name-for-vms dir-name))))
  4572.   
  4573. (or (assq 'vms ange-ftp-fix-dir-name-func-alist)
  4574.     (setq ange-ftp-fix-dir-name-func-alist
  4575.       (cons '(vms . ange-ftp-fix-dir-name-for-vms)
  4576.         ange-ftp-fix-dir-name-func-alist)))
  4577.  
  4578. (defvar ange-ftp-vms-host-regexp nil)
  4579.  
  4580. ;; Return non-nil if HOST is running VMS.
  4581. (defun ange-ftp-vms-host (host)
  4582.   (and ange-ftp-vms-host-regexp
  4583.        (save-match-data
  4584.      (string-match ange-ftp-vms-host-regexp host))))
  4585.  
  4586. ;; Because some VMS ftp servers convert filenames to lower case
  4587. ;; we allow a-z in the filename regexp. I'm not too happy about this.
  4588.  
  4589. (defconst ange-ftp-vms-filename-regexp
  4590.   (concat
  4591.    "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][-_A-Za-z0-9$]*\\)\\."
  4592.    "[-_A-Za-z0-9$]*;+[0-9]*\\)")
  4593.   "Regular expression to match for a valid VMS file name in Dired buffer.
  4594. Stupid freaking bug! Position of _ and $ shouldn't matter but they do.
  4595. Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX
  4596. Other orders of $ and _ seem to all work just fine.")
  4597.  
  4598. ;; These parsing functions are as general as possible because the syntax
  4599. ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that
  4600. ;; the VMS filename syntax is so rigid. If they bomb on a listing in the
  4601. ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing
  4602. ;; from vms.weird.net, then too bad.
  4603.  
  4604. ;; Extract the next filename from a VMS dired-like listing.
  4605. (defun ange-ftp-parse-vms-filename ()
  4606.   (if (re-search-forward
  4607.      ange-ftp-vms-filename-regexp
  4608.      nil t)
  4609.     (buffer-substring (match-beginning 0) (match-end 0))))
  4610.  
  4611. ;; Parse the current buffer which is assumed to be in MultiNet FTP dir
  4612. ;; format, and return a hashtable as the result.
  4613. (defun ange-ftp-parse-vms-listing ()
  4614.   (let ((tbl (ange-ftp-make-hashtable))
  4615.     file)
  4616.     (goto-char (point-min))
  4617.     (save-match-data
  4618.       (while (setq file (ange-ftp-parse-vms-filename))
  4619.     (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file)
  4620.         ;; deal with directories
  4621.         (ange-ftp-put-hash-entry
  4622.          (substring file 0 (match-beginning 0)) t tbl)
  4623.       (ange-ftp-put-hash-entry file nil tbl)
  4624.       (if (string-match ";[0-9]+$" file) ; deal with extension
  4625.           ;; sans extension
  4626.           (ange-ftp-put-hash-entry
  4627.            (substring file 0 (match-beginning 0)) nil tbl)))
  4628.     (forward-line 1))
  4629.       ;; Would like to look for a "Total" line, or a "Directory" line to
  4630.       ;; make sure that the listing isn't complete garbage before putting
  4631.       ;; in "." and "..", but we can't even count on all VAX's giving us
  4632.       ;; either of these.
  4633.        (ange-ftp-put-hash-entry "." t tbl)
  4634.        (ange-ftp-put-hash-entry ".." t tbl))
  4635.     tbl))
  4636.  
  4637. (or (assq 'vms ange-ftp-parse-list-func-alist)
  4638.     (setq ange-ftp-parse-list-func-alist
  4639.       (cons '(vms . ange-ftp-parse-vms-listing)
  4640.         ange-ftp-parse-list-func-alist)))
  4641.  
  4642. ;; This version only deletes file entries which have
  4643. ;; explicit version numbers, because that is all VMS allows.
  4644.  
  4645. ;; Can the following two functions be speeded up using file
  4646. ;; completion functions?
  4647.  
  4648. (defun ange-ftp-vms-delete-file-entry (name &optional dir-p)
  4649.   (if dir-p
  4650.       (ange-ftp-internal-delete-file-entry name t)
  4651.     (save-match-data
  4652.       (let ((file (ange-ftp-get-file-part name)))
  4653.     (if (string-match ";[0-9]+$" file)
  4654.         ;; In VMS you can't delete a file without an explicit    
  4655.         ;; version number, or wild-card (e.g. FOO;*)
  4656.         ;; For now, we give up on wildcards.
  4657.         (let ((files (ange-ftp-get-hash-entry
  4658.               (file-name-directory name)
  4659.               ange-ftp-files-hashtable)))
  4660.           (if files
  4661.           (let* ((root (substring file 0
  4662.                       (match-beginning 0)))
  4663.              (regexp (concat "^"
  4664.                      (regexp-quote root)
  4665.                      ";[0-9]+$"))
  4666.              versions)
  4667.             (ange-ftp-del-hash-entry file files)
  4668.             ;; Now we need to check if there are any
  4669.             ;; versions left. If not, then delete the
  4670.             ;; root entry.
  4671.             (mapatoms
  4672.              '(lambda (sym)
  4673.             (and (string-match regexp (get sym 'key))
  4674.                  (setq versions t)))
  4675.              files)
  4676.             (or versions
  4677.             (ange-ftp-del-hash-entry root files))))))))))
  4678.  
  4679. (or (assq 'vms ange-ftp-delete-file-entry-alist)
  4680.     (setq ange-ftp-delete-file-entry-alist
  4681.       (cons '(vms . ange-ftp-vms-delete-file-entry)
  4682.         ange-ftp-delete-file-entry-alist)))
  4683.  
  4684. (defun ange-ftp-vms-add-file-entry (name &optional dir-p)
  4685.   (if dir-p
  4686.       (ange-ftp-internal-add-file-entry name t)
  4687.     (let ((files (ange-ftp-get-hash-entry
  4688.           (file-name-directory name)
  4689.           ange-ftp-files-hashtable)))
  4690.       (if files
  4691.       (let ((file (ange-ftp-get-file-part name)))
  4692.         (save-match-data
  4693.           (if (string-match ";[0-9]+$" file)
  4694.           (ange-ftp-put-hash-entry
  4695.            (substring file 0 (match-beginning 0))
  4696.            nil files)
  4697.         ;; Need to figure out what version of the file
  4698.         ;; is being added.
  4699.         (let ((regexp (concat "^"
  4700.                       (regexp-quote file)
  4701.                       ";\\([0-9]+\\)$"))
  4702.               (version 0))
  4703.           (mapatoms
  4704.            '(lambda (sym)
  4705.               (let ((name (get sym 'key)))
  4706.             (and (string-match regexp name)
  4707.                  (setq version
  4708.                    (max version
  4709.                     (string-to-int
  4710.                      (substring name
  4711.                             (match-beginning 1)
  4712.                             (match-end 1))))))))
  4713.            files)
  4714.           (setq version (1+ version))
  4715.           (ange-ftp-put-hash-entry
  4716.            (concat file ";" (int-to-string version))
  4717.            nil files))))
  4718.         (ange-ftp-put-hash-entry file nil files))))))
  4719.  
  4720. (or (assq 'vms ange-ftp-add-file-entry-alist)
  4721.     (setq ange-ftp-add-file-entry-alist
  4722.       (cons '(vms . ange-ftp-vms-add-file-entry)
  4723.         ange-ftp-add-file-entry-alist)))
  4724.  
  4725.  
  4726. (defun ange-ftp-add-vms-host (host)
  4727.   "Mark HOST as the name of a machine running VMS."
  4728.   (interactive
  4729.    (list (read-string "Host: "
  4730.               (let ((name (or (buffer-file-name) default-directory)))
  4731.             (and name (car (ange-ftp-ftp-name name)))))))
  4732.   (if (not (ange-ftp-vms-host host))
  4733.       (setq ange-ftp-vms-host-regexp
  4734.         (concat "^" (regexp-quote host) "$"
  4735.             (and ange-ftp-vms-host-regexp "\\|")
  4736.             ange-ftp-vms-host-regexp)
  4737.         ange-ftp-host-cache nil)))
  4738.  
  4739.  
  4740. (defun ange-ftp-vms-file-name-as-directory (name)
  4741.   (save-match-data
  4742.     (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name)
  4743.     (setq name (substring name 0 (match-beginning 0))))
  4744.     (ange-ftp-real-file-name-as-directory name)))
  4745.  
  4746. (or (assq 'vms ange-ftp-file-name-as-directory-alist)
  4747.     (setq ange-ftp-file-name-as-directory-alist
  4748.       (cons '(vms . ange-ftp-vms-file-name-as-directory)
  4749.         ange-ftp-file-name-as-directory-alist)))
  4750.  
  4751. ;;; Tree dired support:
  4752.  
  4753. ;; For this code I have borrowed liberally from Sebastian Kremer's
  4754. ;; dired-vms.el
  4755.  
  4756.  
  4757. ;;;; These regexps must be anchored to beginning of line.
  4758. ;;;; Beware that the ftpd may put the device in front of the filename.
  4759.  
  4760. ;;(defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]"
  4761. ;;  "Regular expression to use to search for VMS executable files.")
  4762.  
  4763. ;;(defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]"
  4764. ;;  "Regular expression to use to search for VMS directories.")
  4765.  
  4766. ;;(or (assq 'vms ange-ftp-dired-re-exe-alist)
  4767. ;;    (setq ange-ftp-dired-re-exe-alist
  4768. ;;      (cons (cons 'vms  ange-ftp-dired-vms-re-exe)
  4769. ;;        ange-ftp-dired-re-exe-alist)))
  4770.  
  4771. ;;(or (assq 'vms ange-ftp-dired-re-dir-alist)
  4772. ;;    (setq ange-ftp-dired-re-dir-alist
  4773. ;;      (cons (cons 'vms  ange-ftp-dired-vms-re-dir)
  4774. ;;        ange-ftp-dired-re-dir-alist)))
  4775.  
  4776. ;;(defun ange-ftp-dired-vms-insert-headerline (dir)
  4777. ;;  ;; VMS inserts a headerline. I would prefer the headerline
  4778. ;;  ;; to be in ange-ftp format. This version tries to
  4779. ;;  ;; be careful, because we can't count on a headerline
  4780. ;;  ;; over ftp, and we wouldn't want to delete anything
  4781. ;;  ;; important.
  4782. ;;  (save-excursion
  4783. ;;    (if (looking-at "^  wildcard ")
  4784. ;;    (forward-line 1))
  4785. ;;    (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n")
  4786. ;;    (delete-region (point) (match-end 0))))
  4787. ;;  (ange-ftp-real-dired-insert-headerline dir))
  4788.  
  4789. ;;(or (assq 'vms ange-ftp-dired-insert-headerline-alist)
  4790. ;;    (setq ange-ftp-dired-insert-headerline-alist
  4791. ;;      (cons '(vms . ange-ftp-dired-vms-insert-headerline)
  4792. ;;        ange-ftp-dired-insert-headerline-alist)))
  4793.  
  4794. ;;(defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol)
  4795. ;;  "In dired, move to first char of filename on this line.
  4796. ;;Returns position (point) or nil if no filename on this line."
  4797. ;;  ;; This is the VMS version.
  4798. ;;  (let (case-fold-search)
  4799. ;;    (or eol (setq eol (progn (end-of-line) (point))))
  4800. ;;    (beginning-of-line)
  4801. ;;    (if (re-search-forward ange-ftp-vms-filename-regexp eol t)
  4802. ;;    (goto-char (match-beginning 1))
  4803. ;;      (if raise-error
  4804. ;;      (error "No file on this line")
  4805. ;;    nil))))
  4806.  
  4807. ;;(or (assq 'vms ange-ftp-dired-move-to-filename-alist)
  4808. ;;    (setq ange-ftp-dired-move-to-filename-alist
  4809. ;;      (cons '(vms . ange-ftp-dired-vms-move-to-filename)
  4810. ;;        ange-ftp-dired-move-to-filename-alist)))
  4811.  
  4812. ;;(defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol)
  4813. ;;  ;; Assumes point is at beginning of filename.
  4814. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  4815. ;;  ;; case-fold-search must be nil, at least for VMS.
  4816. ;;  ;; On failure, signals an error or returns nil.
  4817. ;;  ;; This is the VMS version.
  4818. ;;  (let (opoint hidden case-fold-search)
  4819. ;;    (setq opoint (point))
  4820. ;;    (or eol (setq eol (save-excursion (end-of-line) (point))))
  4821. ;;    (setq hidden (and selective-display
  4822. ;;              (save-excursion (search-forward "\r" eol t))))
  4823. ;;    (if hidden
  4824. ;;    nil
  4825. ;;      (re-search-forward ange-ftp-vms-filename-regexp eol t))
  4826. ;;    (or no-error
  4827. ;;    (not (eq opoint (point)))
  4828. ;;    (error
  4829. ;;     (if hidden
  4830. ;;         (substitute-command-keys
  4831. ;;          "File line is hidden, type \\[dired-hide-subdir] to unhide")
  4832. ;;       "No file on this line")))
  4833. ;;    (if (eq opoint (point))
  4834. ;;    nil
  4835. ;;      (point))))
  4836.  
  4837. ;;(or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist)
  4838. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  4839. ;;      (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename)
  4840. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  4841.  
  4842. ;;(defun ange-ftp-dired-vms-between-files ()
  4843. ;;  (save-excursion
  4844. ;;    (beginning-of-line)
  4845. ;;    (or (equal (following-char) 10) ; newline
  4846. ;;     (equal (following-char) 9)     ; tab
  4847. ;;     (progn (forward-char 2)
  4848. ;;        (or (looking-at "Total of")
  4849. ;;        (equal (following-char) 32))))))
  4850.  
  4851. ;;(or (assq 'vms ange-ftp-dired-between-files-alist)
  4852. ;;    (setq ange-ftp-dired-between-files-alist
  4853. ;;      (cons '(vms . ange-ftp-dired-vms-between-files)
  4854. ;;        ange-ftp-dired-between-files-alist)))
  4855.  
  4856. ;; Beware! In VMS filenames must be of the form "FILE.TYPE".
  4857. ;; Therefore, we cannot just append a ".Z" to filenames for
  4858. ;; compressed files. Instead, we turn "FILE.TYPE" into
  4859. ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do.
  4860.  
  4861. (defun ange-ftp-vms-make-compressed-filename (name &optional reverse)
  4862.   (cond
  4863.    ((string-match "-Z;[0-9]+$" name)
  4864.     (list nil (substring name 0 (match-beginning 0))))
  4865.    ((string-match ";[0-9]+$" name)
  4866.     (list nil (substring name 0 (match-beginning 0))))
  4867.    ((string-match "-Z$" name)
  4868.     (list nil (substring name 0 -2)))
  4869.    (t
  4870.     (list t
  4871.       (if (string-match ";[0-9]+$" name)
  4872.           (concat (substring name 0 (match-beginning 0))
  4873.               "-Z")
  4874.         (concat name "-Z"))))))
  4875.  
  4876. (or (assq 'vms ange-ftp-make-compressed-filename-alist)
  4877.     (setq ange-ftp-make-compressed-filename-alist
  4878.       (cons '(vms . ange-ftp-vms-make-compressed-filename)
  4879.         ange-ftp-make-compressed-filename-alist)))
  4880.  
  4881. ;;;; When the filename is too long, VMS will use two lines to list a file
  4882. ;;;; (damn them!) This will confuse dired. To solve this, need to convince
  4883. ;;;; Sebastian to use a function dired-go-to-end-of-file-line, instead of
  4884. ;;;; (forward-line 1). This would require a number of changes to dired.el.
  4885. ;;;; If dired gets confused, revert-buffer will fix it.
  4886.  
  4887. ;;(defun ange-ftp-dired-vms-ls-trim ()
  4888. ;;  (goto-char (point-min))
  4889. ;;  (let ((case-fold-search nil))
  4890. ;;    (re-search-forward  ange-ftp-vms-filename-regexp))
  4891. ;;  (beginning-of-line)
  4892. ;;  (delete-region (point-min) (point))
  4893. ;;  (forward-line 1)
  4894. ;;  (delete-region (point) (point-max)))
  4895.  
  4896.  
  4897. ;;(or (assq 'vms ange-ftp-dired-ls-trim-alist)
  4898. ;;    (setq ange-ftp-dired-ls-trim-alist
  4899. ;;      (cons '(vms . ange-ftp-dired-vms-ls-trim)
  4900. ;;        ange-ftp-dired-ls-trim-alist)))    
  4901.  
  4902. (defun ange-ftp-vms-sans-version (name &rest args)
  4903.   (save-match-data
  4904.     (if (string-match ";[0-9]+$" name)
  4905.     (substring name 0 (match-beginning 0))
  4906.       name)))
  4907.  
  4908. (or (assq 'vms ange-ftp-sans-version-alist)
  4909.     (setq ange-ftp-sans-version-alist
  4910.       (cons '(vms . ange-ftp-vms-sans-version)
  4911.         ange-ftp-sans-version-alist)))
  4912.  
  4913. ;;(defvar ange-ftp-file-version-alist)
  4914.  
  4915. ;;;;; The vms version of clean-directory has 2 more optional args
  4916. ;;;;; than the usual dired version. This is so that it can be used by
  4917. ;;;;; ange-ftp-dired-vms-flag-backup-files.
  4918.  
  4919. ;;(defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg)
  4920. ;;  "Flag numerical backups for deletion.
  4921. ;;Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
  4922. ;;Positive prefix arg KEEP overrides `dired-kept-versions';
  4923. ;;Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
  4924.  
  4925. ;;To clear the flags on these files, you can use \\[dired-flag-backup-files]
  4926. ;;with a prefix argument."
  4927. ;;;  (interactive "P") ; Never actually called interactively.
  4928. ;;  (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions)))
  4929. ;;  (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
  4930. ;;    ;; late-retention must NEVER be allowed to be less than 1 in VMS!
  4931. ;;    ;; This could wipe ALL copies of the file.
  4932. ;;    (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep)))
  4933. ;;    (action (or msg "Cleaning"))
  4934. ;;    (ange-ftp-trample-marker (or marker dired-del-marker))
  4935. ;;    (ange-ftp-file-version-alist ()))
  4936. ;;    (message (concat action
  4937. ;;             " numerical backups (keeping %d late, %d old)...")
  4938. ;;         late-retention early-retention)
  4939. ;;    ;; Look at each file.
  4940. ;;    ;; If the file has numeric backup versions,
  4941. ;;    ;; put on ange-ftp-file-version-alist an element of the form
  4942. ;;    ;; (FILENAME . VERSION-NUMBER-LIST)
  4943. ;;    (dired-map-dired-file-lines (function
  4944. ;;                 ange-ftp-dired-vms-collect-file-versions))
  4945. ;;    ;; Sort each VERSION-NUMBER-LIST,
  4946. ;;    ;; and remove the versions not to be deleted.
  4947. ;;    (let ((fval ange-ftp-file-version-alist))
  4948. ;;      (while fval
  4949. ;;    (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
  4950. ;;           (v-count (length sorted-v-list)))
  4951. ;;      (if (> v-count (+ early-retention late-retention))
  4952. ;;          (rplacd (nthcdr early-retention sorted-v-list)
  4953. ;;              (nthcdr (- v-count late-retention)
  4954. ;;                  sorted-v-list)))
  4955. ;;      (rplacd (car fval)
  4956. ;;          (cdr sorted-v-list)))
  4957. ;;    (setq fval (cdr fval))))
  4958. ;;    ;; Look at each file.  If it is a numeric backup file,
  4959. ;;    ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
  4960. ;;    (dired-map-dired-file-lines
  4961. ;;     (function
  4962. ;;      ange-ftp-dired-vms-trample-file-versions mark))
  4963. ;;    (message (concat action " numerical backups...done"))))
  4964.  
  4965. ;;(or (assq 'vms ange-ftp-dired-clean-directory-alist)
  4966. ;;    (setq ange-ftp-dired-clean-directory-alist
  4967. ;;      (cons '(vms . ange-ftp-dired-vms-clean-directory)
  4968. ;;        ange-ftp-dired-clean-directory-alist)))
  4969.  
  4970. ;;(defun ange-ftp-dired-vms-collect-file-versions (fn)
  4971. ;;  ;;  "If it looks like file FN has versions, return a list of the versions.
  4972. ;;  ;;That is a list of strings which are file names.
  4973. ;;  ;;The caller may want to flag some of these files for deletion."
  4974. ;;(let ((name (nth 2 (ange-ftp-ftp-name fn))))
  4975. ;;  (if (string-match ";[0-9]+$" name)
  4976. ;;      (let* ((name (substring name 0 (match-beginning 0)))
  4977. ;;         (fn (ange-ftp-replace-name-component fn name)))
  4978. ;;    (if (not (assq fn ange-ftp-file-version-alist))
  4979. ;;        (let* ((base-versions
  4980. ;;            (concat (file-name-nondirectory name) ";"))
  4981. ;;           (bv-length (length base-versions))
  4982. ;;           (possibilities (file-name-all-completions
  4983. ;;                   base-versions
  4984. ;;                   (file-name-directory fn)))
  4985. ;;           (versions (mapcar
  4986. ;;                  '(lambda (arg)
  4987. ;;                 (if (and (string-match
  4988. ;;                       "[0-9]+$" arg bv-length)
  4989. ;;                      (= (match-beginning 0) bv-length))
  4990. ;;                     (string-to-int (substring arg bv-length))
  4991. ;;                   0))
  4992. ;;                  possibilities)))
  4993. ;;          (if versions
  4994. ;;          (setq
  4995. ;;           ange-ftp-file-version-alist
  4996. ;;           (cons (cons fn versions)
  4997. ;;             ange-ftp-file-version-alist)))))))))
  4998.  
  4999. ;;(defun ange-ftp-dired-vms-trample-file-versions (fn)
  5000. ;;  (let* ((start-vn (string-match ";[0-9]+$" fn))
  5001. ;;     base-version-list)
  5002. ;;    (and start-vn
  5003. ;;     (setq base-version-list    ; there was a base version to which
  5004. ;;           (assoc (substring fn 0 start-vn)    ; this looks like a
  5005. ;;              ange-ftp-file-version-alist))    ; subversion
  5006. ;;     (not (memq (string-to-int (substring fn (1+ start-vn)))
  5007. ;;            base-version-list))    ; this one doesn't make the cut
  5008. ;;     (progn (beginning-of-line)
  5009. ;;        (delete-char 1)
  5010. ;;        (insert ange-ftp-trample-marker)))))
  5011.  
  5012. ;;(defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p)
  5013. ;;  (let ((dired-kept-versions 1)
  5014. ;;    (kept-old-versions 0)
  5015. ;;    marker msg)
  5016. ;;    (if unflag-p
  5017. ;;    (setq marker ?\040 msg "Unflagging")
  5018. ;;      (setq marker dired-del-marker msg "Cleaning"))
  5019. ;;    (ange-ftp-dired-vms-clean-directory nil marker msg)))
  5020.  
  5021. ;;(or (assq 'vms ange-ftp-dired-flag-backup-files-alist)
  5022. ;;    (setq ange-ftp-dired-flag-backup-files-alist
  5023. ;;      (cons '(vms . ange-ftp-dired-vms-flag-backup-files)
  5024. ;;        ange-ftp-dired-flag-backup-files-alist)))
  5025.  
  5026. ;;(defun ange-ftp-dired-vms-backup-diff (&optional switches)
  5027. ;;  (let ((file (dired-get-filename 'no-dir))
  5028. ;;    bak)
  5029. ;;    (if (and (string-match ";[0-9]+$" file)
  5030. ;;         ;; Find most recent previous version.
  5031. ;;         (let ((root (substring file 0 (match-beginning 0)))
  5032. ;;           (ver
  5033. ;;            (string-to-int (substring file (1+ (match-beginning 0)))))
  5034. ;;           found)
  5035. ;;           (setq ver (1- ver))
  5036. ;;           (while (and (> ver 0) (not found))
  5037. ;;         (setq bak (concat root ";" (int-to-string ver)))
  5038. ;;         (and (file-exists-p bak) (setq found t))
  5039. ;;         (setq ver (1- ver)))
  5040. ;;           found))
  5041. ;;    (if switches
  5042. ;;        (diff (expand-file-name bak) (expand-file-name file) switches)
  5043. ;;      (diff (expand-file-name bak) (expand-file-name file)))
  5044. ;;      (error "No previous version found for %s" file))))
  5045.  
  5046. ;;(or (assq 'vms ange-ftp-dired-backup-diff-alist)
  5047. ;;    (setq ange-ftp-dired-backup-diff-alist
  5048. ;;      (cons '(vms . ange-ftp-dired-vms-backup-diff)
  5049. ;;        ange-ftp-dired-backup-diff-alist)))
  5050.  
  5051.  
  5052. ;;;; ------------------------------------------------------------
  5053. ;;;; MTS support
  5054. ;;;; ------------------------------------------------------------
  5055.  
  5056.  
  5057. ;; Convert NAME from UNIX-ish to MTS. If REVERSE given then convert from
  5058. ;; MTS to UNIX-ish.
  5059. (defun ange-ftp-fix-name-for-mts (name &optional reverse)
  5060.   (save-match-data
  5061.     (if reverse
  5062.     (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" name)
  5063.         (let (acct file)
  5064.           (if (match-beginning 1)
  5065.           (setq acct (substring name 0 (match-end 1))))
  5066.           (if (match-beginning 2)
  5067.           (setq file (substring name
  5068.                     (match-beginning 2) (match-end 2))))
  5069.           (concat (and acct (concat "/" acct "/"))
  5070.               file))
  5071.       (error "name %s didn't match" name))
  5072.       (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" name)
  5073.       (concat (substring name 1 (match-end 1))
  5074.           (substring name (match-beginning 2) (match-end 2)))
  5075.     ;; Let's hope that mts will recognize it anyway.
  5076.     name))))
  5077.  
  5078. (or (assq 'mts ange-ftp-fix-name-func-alist)
  5079.     (setq ange-ftp-fix-name-func-alist
  5080.       (cons '(mts . ange-ftp-fix-name-for-mts)
  5081.         ange-ftp-fix-name-func-alist)))
  5082.  
  5083. ;; Convert name from UNIX-ish to MTS ready for a DIRectory listing.
  5084. ;; Remember that there are no directories in MTS.
  5085. (defun ange-ftp-fix-dir-name-for-mts (dir-name)
  5086.   (if (string-equal dir-name "/")
  5087.       (error "Cannot get listing for fictitious \"/\" directory.")
  5088.     (let ((dir-name (ange-ftp-fix-name-for-mts dir-name)))
  5089.       (cond
  5090.        ((string-equal dir-name "")
  5091.     "?")
  5092.        ((string-match ":$" dir-name)
  5093.     (concat dir-name "?"))
  5094.        (dir-name))))) ; It's just a single file.
  5095.  
  5096. (or (assq 'mts ange-ftp-fix-dir-name-func-alist)
  5097.     (setq ange-ftp-fix-dir-name-func-alist
  5098.       (cons '(mts . ange-ftp-fix-dir-name-for-mts)
  5099.         ange-ftp-fix-dir-name-func-alist)))
  5100.  
  5101. (or (memq 'mts ange-ftp-dumb-host-types)
  5102.     (setq ange-ftp-dumb-host-types
  5103.       (cons 'mts ange-ftp-dumb-host-types)))
  5104.  
  5105. (defvar ange-ftp-mts-host-regexp nil)
  5106.  
  5107. ;; Return non-nil if HOST is running MTS.
  5108. (defun ange-ftp-mts-host (host)
  5109.   (and ange-ftp-mts-host-regexp
  5110.        (save-match-data
  5111.      (string-match ange-ftp-mts-host-regexp host))))
  5112.  
  5113. ;; Parse the current buffer which is assumed to be in mts ftp dir format.
  5114. (defun ange-ftp-parse-mts-listing ()
  5115.   (let ((tbl (ange-ftp-make-hashtable)))
  5116.     (goto-char (point-min))
  5117.     (save-match-data
  5118.       (while (re-search-forward ange-ftp-date-regexp nil t)
  5119.     (end-of-line)
  5120.     (skip-chars-backward " ")
  5121.     (let ((end (point)))
  5122.       (skip-chars-backward "-A-Z0-9_.!")
  5123.       (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl))
  5124.     (forward-line 1)))
  5125.       ;; Don't need to bother with ..
  5126.     (ange-ftp-put-hash-entry "." t tbl)
  5127.     tbl))
  5128.  
  5129. (or (assq 'mts ange-ftp-parse-list-func-alist)
  5130.     (setq ange-ftp-parse-list-func-alist
  5131.       (cons '(mts . ange-ftp-parse-mts-listing)
  5132.         ange-ftp-parse-list-func-alist)))
  5133.  
  5134. (defun ange-ftp-add-mts-host (host)
  5135.   "Mark HOST as the name of a machine running MTS."
  5136.   (interactive
  5137.    (list (read-string "Host: "
  5138.               (let ((name (or (buffer-file-name) default-directory)))
  5139.             (and name (car (ange-ftp-ftp-name name)))))))
  5140.   (if (not (ange-ftp-mts-host host))
  5141.       (setq ange-ftp-mts-host-regexp
  5142.         (concat "^" (regexp-quote host) "$"
  5143.             (and ange-ftp-mts-host-regexp "\\|")
  5144.             ange-ftp-mts-host-regexp)
  5145.         ange-ftp-host-cache nil)))
  5146.  
  5147. ;;; Tree dired support:
  5148.  
  5149. ;;;; There aren't too many systems left that use MTS. This dired support will
  5150. ;;;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems
  5151. ;;;; implement ftp in the same way. If not, it might be necessary to make the
  5152. ;;;; following more flexible.
  5153.  
  5154. ;;(defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol)
  5155. ;;  "In dired, move to first char of filename on this line.
  5156. ;;Returns position (point) or nil if no filename on this line."
  5157. ;;  ;; This is the MTS version.
  5158. ;;  (or eol (setq eol (progn (end-of-line) (point))))
  5159. ;;  (beginning-of-line)
  5160. ;;  (if (re-search-forward
  5161. ;;       ange-ftp-date-regexp eol t)
  5162. ;;      (progn
  5163. ;;    (skip-chars-forward " ")          ; Eat blanks after date
  5164. ;;    (skip-chars-forward "0-9:" eol)   ; Eat time or year
  5165. ;;    (skip-chars-forward " " eol)      ; one space before filename
  5166. ;;    ;; When listing an account other than the users own account it appends
  5167. ;;    ;; ACCT: to the beginning of the filename. Skip over this.
  5168. ;;    (and (looking-at "[A-Z0-9_.]+:")
  5169. ;;         (goto-char (match-end 0)))
  5170. ;;    (point))
  5171. ;;    (if raise-error
  5172. ;;    (error "No file on this line")
  5173. ;;      nil)))
  5174.  
  5175. ;;(or (assq 'mts ange-ftp-dired-move-to-filename-alist)
  5176. ;;    (setq ange-ftp-dired-move-to-filename-alist
  5177. ;;      (cons '(mts . ange-ftp-dired-mts-move-to-filename)
  5178. ;;        ange-ftp-dired-move-to-filename-alist)))
  5179.  
  5180. ;;(defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol)
  5181. ;;  ;; Assumes point is at beginning of filename.
  5182. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  5183. ;;  ;; On failure, signals an error or returns nil.
  5184. ;;  ;; This is the MTS version.
  5185. ;;  (let (opoint hidden case-fold-search)
  5186. ;;    (setq opoint (point)
  5187. ;;      eol (save-excursion (end-of-line) (point))
  5188. ;;      hidden (and selective-display
  5189. ;;              (save-excursion (search-forward "\r" eol t))))
  5190. ;;    (if hidden
  5191. ;;    nil
  5192. ;;      (skip-chars-forward "-A-Z0-9._!" eol))
  5193. ;;    (or no-error
  5194. ;;    (not (eq opoint (point)))
  5195. ;;    (error
  5196. ;;     (if hidden
  5197. ;;         (substitute-command-keys
  5198. ;;          "File line is hidden, type \\[dired-hide-subdir] to unhide")
  5199. ;;       "No file on this line")))
  5200. ;;    (if (eq opoint (point))
  5201. ;;    nil
  5202. ;;      (point))))
  5203.  
  5204. ;;(or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist)
  5205. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  5206. ;;      (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename)
  5207. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  5208.  
  5209. ;;;; ------------------------------------------------------------
  5210. ;;;; CMS support
  5211. ;;;; ------------------------------------------------------------
  5212.  
  5213. ;; Since CMS doesn't have any full file name syntax, we have to fudge
  5214. ;; things with cd's. We actually send too many cd's, but it's dangerous
  5215. ;; to try to remember the current minidisk, because if the connection
  5216. ;; is closed and needs to be reopened, we will find ourselves back in
  5217. ;; the default minidisk. This is fairly likely since CMS ftp servers
  5218. ;; usually close the connection after 5 minutes of inactivity.
  5219.  
  5220. ;; Have I got the filename character set right?
  5221.  
  5222. (defun ange-ftp-fix-name-for-cms (name &optional reverse)
  5223.   (save-match-data
  5224.     (if reverse
  5225.     ;; Since we only convert output from a pwd in this direction,
  5226.     ;; we'll assume that it's a minidisk, and make it into a
  5227.     ;; directory file name. Note that the expand-dir-hashtable
  5228.     ;; stores directories without the trailing /. Is this
  5229.     ;; consistent?
  5230.     (concat "/" name)
  5231.       (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$"
  5232.             name)
  5233.       (let ((minidisk (substring name 1 (match-end 1))))
  5234.         (if (match-beginning 2)
  5235.         (let ((file (substring name (match-beginning 2)
  5236.                        (match-end 2)))
  5237.               (cmd (concat "cd " minidisk))
  5238.  
  5239.               ;; Note that host and user are bound in the call
  5240.               ;; to ange-ftp-send-cmd
  5241.               (proc (ange-ftp-get-process ange-ftp-this-host
  5242.                           ange-ftp-this-user)))
  5243.  
  5244.           ;; Must use ange-ftp-raw-send-cmd here to avoid
  5245.           ;; an infinite loop.
  5246.           (if (car (ange-ftp-raw-send-cmd proc cmd ange-ftp-this-msg))
  5247.               file
  5248.             ;; failed... try ONCE more.
  5249.             (setq proc (ange-ftp-get-process ange-ftp-this-host
  5250.                              ange-ftp-this-user))
  5251.             (let ((result (ange-ftp-raw-send-cmd proc cmd
  5252.                              ange-ftp-this-msg)))
  5253.               (if (car result)
  5254.               file
  5255.             ;; failed.  give up.
  5256.             (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
  5257.                     (format "cd to minidisk %s failed: %s"
  5258.                         minidisk (cdr result)))))))
  5259.           ;; return the minidisk
  5260.           minidisk))
  5261.     (error "Invalid CMS filename")))))
  5262.  
  5263. (or (assq 'cms ange-ftp-fix-name-func-alist)
  5264.     (setq ange-ftp-fix-name-func-alist
  5265.       (cons '(cms . ange-ftp-fix-name-for-cms)
  5266.         ange-ftp-fix-name-func-alist)))
  5267.  
  5268. (or (memq 'cms ange-ftp-dumb-host-types)
  5269.     (setq ange-ftp-dumb-host-types
  5270.       (cons 'cms ange-ftp-dumb-host-types)))
  5271.  
  5272. ;; Convert name from UNIX-ish to CMS ready for a DIRectory listing.
  5273. (defun ange-ftp-fix-dir-name-for-cms (dir-name)
  5274.   (cond
  5275.    ((string-equal "/" dir-name)
  5276.     (error "Cannot get listing for fictitious \"/\" directory."))
  5277.    ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-name)
  5278.     (let* ((minidisk (substring dir-name (match-beginning 1) (match-end 1)))
  5279.        ;; host and user are bound in the call to ange-ftp-send-cmd
  5280.        (proc (ange-ftp-get-process ange-ftp-this-host ange-ftp-this-user))
  5281.        (cmd (concat "cd " minidisk))
  5282.        (file (if (match-beginning 2)
  5283.              ;; it's a single file
  5284.              (substring dir-name (match-beginning 2)
  5285.                 (match-end 2))
  5286.            ;; use the wild-card
  5287.            "*")))
  5288.       (if (car (ange-ftp-raw-send-cmd proc cmd))
  5289.       file
  5290.     ;; try again...
  5291.     (setq proc (ange-ftp-get-process ange-ftp-this-host
  5292.                      ange-ftp-this-user))
  5293.     (let ((result (ange-ftp-raw-send-cmd proc cmd)))
  5294.       (if (car result)
  5295.           file
  5296.         ;; give up
  5297.         (ange-ftp-error ange-ftp-this-host ange-ftp-this-user
  5298.                 (format "cd to minidisk %s failed: %s"
  5299.                     minidisk (cdr result))))))))
  5300.    (t (error "Invalid CMS file name"))))
  5301.  
  5302. (or (assq 'cms ange-ftp-fix-dir-name-func-alist)
  5303.     (setq ange-ftp-fix-dir-name-func-alist
  5304.       (cons '(cms . ange-ftp-fix-dir-name-for-cms)
  5305.         ange-ftp-fix-dir-name-func-alist)))
  5306.  
  5307. (defvar ange-ftp-cms-host-regexp nil
  5308.   "Regular expression to match hosts running the CMS operating system.")
  5309.  
  5310. ;; Return non-nil if HOST is running CMS.
  5311. (defun ange-ftp-cms-host (host)
  5312.   (and ange-ftp-cms-host-regexp
  5313.        (save-match-data
  5314.      (string-match ange-ftp-cms-host-regexp host))))
  5315.  
  5316. (defun ange-ftp-add-cms-host (host)
  5317.   "Mark HOST as the name of a CMS host."
  5318.   (interactive
  5319.    (list (read-string "Host: "
  5320.               (let ((name (or (buffer-file-name) default-directory)))
  5321.             (and name (car (ange-ftp-ftp-name name)))))))
  5322.   (if (not (ange-ftp-cms-host host))
  5323.       (setq ange-ftp-cms-host-regexp
  5324.         (concat "^" (regexp-quote host) "$"
  5325.             (and ange-ftp-cms-host-regexp "\\|")
  5326.             ange-ftp-cms-host-regexp)
  5327.         ange-ftp-host-cache nil)))
  5328.  
  5329. (defun ange-ftp-parse-cms-listing ()
  5330.   ;; Parse the current buffer which is assumed to be a CMS directory listing.
  5331.   ;; If we succeed in getting a listing, then we will assume that the minidisk
  5332.   ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work
  5333.   ;; because ange-ftp doesn't know that the root hashtable has only part of
  5334.   ;; the info. It will assume that if a minidisk isn't in it, then it doesn't
  5335.   ;; exist. It would be nice if completion worked for minidisks, as we
  5336.   ;; discover them.
  5337. ;  (let* ((dir-file (directory-file-name file))
  5338. ;     (root (file-name-directory dir-file))
  5339. ;     (minidisk (ange-ftp-get-file-part dir-file))
  5340. ;     (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable)))
  5341. ;    (if root-tbl
  5342. ;    (ange-ftp-put-hash-entry minidisk t root-tbl)
  5343. ;      (setq root-tbl (ange-ftp-make-hashtable))
  5344. ;      (ange-ftp-put-hash-entry minidisk t root-tbl)
  5345. ;      (ange-ftp-put-hash-entry "." t root-tbl)
  5346. ;      (ange-ftp-set-files root root-tbl)))
  5347.   ;; Now do the usual parsing
  5348.   (let ((tbl (ange-ftp-make-hashtable)))
  5349.     (goto-char (point-min))
  5350.     (save-match-data
  5351.       (while
  5352.       (re-search-forward
  5353.        "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t)
  5354.     (ange-ftp-put-hash-entry
  5355.      (concat (buffer-substring (match-beginning 1)
  5356.                    (match-end 1))
  5357.          "."
  5358.          (buffer-substring (match-beginning 2)
  5359.                    (match-end 2)))
  5360.      nil tbl)
  5361.     (forward-line 1))
  5362.       (ange-ftp-put-hash-entry "." t tbl))
  5363.     tbl))
  5364.  
  5365. (or (assq 'cms ange-ftp-parse-list-func-alist)
  5366.     (setq ange-ftp-parse-list-func-alist
  5367.       (cons '(cms . ange-ftp-parse-cms-listing)
  5368.         ange-ftp-parse-list-func-alist)))
  5369.     
  5370. ;;;;; Tree dired support:
  5371.  
  5372. ;;(defconst ange-ftp-dired-cms-re-exe
  5373. ;;  "^. [-A-Z0-9$_]+ +EXEC "
  5374. ;;  "Regular expression to use to search for CMS executables.")
  5375.  
  5376. ;;(or (assq 'cms ange-ftp-dired-re-exe-alist)
  5377. ;;    (setq ange-ftp-dired-re-exe-alist
  5378. ;;      (cons (cons 'cms  ange-ftp-dired-cms-re-exe)
  5379. ;;        ange-ftp-dired-re-exe-alist)))
  5380.  
  5381.  
  5382. ;;(defun ange-ftp-dired-cms-insert-headerline (dir)
  5383. ;;  ;; CMS has no total line, so we insert a blank line for
  5384. ;;  ;; aesthetics.
  5385. ;;  (insert "\n")
  5386. ;;  (forward-char -1)
  5387. ;;  (ange-ftp-real-dired-insert-headerline dir))
  5388.  
  5389. ;;(or (assq 'cms ange-ftp-dired-insert-headerline-alist)
  5390. ;;    (setq ange-ftp-dired-insert-headerline-alist
  5391. ;;      (cons '(cms . ange-ftp-dired-cms-insert-headerline)
  5392. ;;        ange-ftp-dired-insert-headerline-alist)))
  5393.  
  5394. ;;(defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol)
  5395. ;;  "In dired, move to the first char of filename on this line."
  5396. ;;  ;; This is the CMS version.
  5397. ;;  (or eol (setq eol (progn (end-of-line) (point))))
  5398. ;;  (let (case-fold-search)
  5399. ;;    (beginning-of-line)
  5400. ;;    (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t)
  5401. ;;    (goto-char (1+ (match-beginning 0)))
  5402. ;;      (if raise-error
  5403. ;;      (error "No file on this line")
  5404. ;;    nil))))
  5405.  
  5406. ;;(or (assq 'cms ange-ftp-dired-move-to-filename-alist)
  5407. ;;    (setq ange-ftp-dired-move-to-filename-alist
  5408. ;;      (cons '(cms . ange-ftp-dired-cms-move-to-filename)
  5409. ;;        ange-ftp-dired-move-to-filename-alist)))
  5410.  
  5411. ;;(defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol)
  5412. ;;  ;; Assumes point is at beginning of filename.
  5413. ;;  ;; So, it should be called only after (dired-move-to-filename t).
  5414. ;;  ;; case-fold-search must be nil, at least for VMS.
  5415. ;;  ;; On failure, signals an error or returns nil.
  5416. ;;  ;; This is the CMS version.
  5417. ;;  (let ((opoint (point))
  5418. ;;    case-fold-search hidden)
  5419. ;;    (or eol (setq eol (save-excursion (end-of-line) (point))))
  5420. ;;    (setq hidden (and selective-display
  5421. ;;              (save-excursion
  5422. ;;            (search-forward "\r" eol t))))
  5423. ;;    (if hidden
  5424. ;;    (if no-error
  5425. ;;        nil
  5426. ;;      (error
  5427. ;;       (substitute-command-keys
  5428. ;;        "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  5429. ;;      (skip-chars-forward "-A-Z0-9$_" eol)
  5430. ;;      (skip-chars-forward " " eol)
  5431. ;;      (skip-chars-forward "-A-Z0-9$_" eol)
  5432. ;;      (if (eq opoint (point))
  5433. ;;      (if no-error
  5434. ;;          nil
  5435. ;;        (error "No file on this line"))
  5436. ;;    (point)))))
  5437.  
  5438. ;;(or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist)
  5439. ;;    (setq ange-ftp-dired-move-to-end-of-filename-alist
  5440. ;;      (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename)
  5441. ;;        ange-ftp-dired-move-to-end-of-filename-alist)))
  5442.  
  5443. (defun ange-ftp-cms-make-compressed-filename (name &optional reverse)
  5444.   (if (string-match "-Z$" name)
  5445.       (list nil (substring name 0 -2))
  5446.     (list t (concat name "-Z"))))
  5447.  
  5448. (or (assq 'cms ange-ftp-make-compressed-filename-alist)
  5449.     (setq ange-ftp-make-compressed-filename-alist
  5450.       (cons '(cms . ange-ftp-cms-make-compressed-filename)
  5451.         ange-ftp-make-compressed-filename-alist)))
  5452.  
  5453. ;;(defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep)
  5454. ;;  (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep)))
  5455. ;;    (and name
  5456. ;;     (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name)
  5457. ;;         (concat (substring name 0 (match-end 1))
  5458. ;;             "."
  5459. ;;             (substring name (match-beginning 2) (match-end 2)))
  5460. ;;       name))))
  5461.  
  5462. ;;(or (assq 'cms ange-ftp-dired-get-filename-alist)
  5463. ;;    (setq ange-ftp-dired-get-filename-alist
  5464. ;;      (cons '(cms . ange-ftp-dired-cms-get-filename)
  5465. ;;        ange-ftp-dired-get-filename-alist)))
  5466.  
  5467. ;;;; ------------------------------------------------------------
  5468. ;;;; Finally provide package.
  5469. ;;;; ------------------------------------------------------------
  5470.  
  5471. (provide 'ange-ftp)
  5472.  
  5473. ;;; ange-ftp.el ends here
  5474.